Hello to everybody.

I'm trying to solve an ACM task #755 called . It's about telephone numbers, quite straightforward. But still something is wrong in my code, because the automatic control says "Wrong answer" and I can't find on which input it fails.

I tested it on Windows using MinGw. I fed big files (100 000 numbers) to it and it worked.

Please help me, I'm desperate now.

The code is:

#include <cstdio>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

using namespace std;

int main() 
{
	int set_count;
	string s;
	getline(cin, s);
	sscanf(s.c_str(), "%d", &set_count);	

	for (int j = 0; j < set_count; j++)
	{
		getline(cin, s);
		map<int,int> telnums;

		int count;
		getline(cin, s);
		sscanf(s.c_str(), "%d", &count);						

		for (int i = 0; i < count; i++)
		{				
			int cur_num = 0;
			getline(cin, s);
			for (unsigned int k = 0; k < s.length(); k++)
			{				
				if (s[k] == '-') continue;
				int digit;
				switch (s[k])
				{
				case '0': digit = 0; break;
				case '1': digit = 1; break;
				case '2': 
				case 'A': 
				case 'B': 
				case 'C': digit = 2; break;
				case '3': 
				case 'D': 
				case 'E': 
				case 'F': digit = 3; break;
				case '4': 
				case 'G': 
				case 'H': 
				case 'I': digit = 4; break;
				case '5': 
				case 'J': 
				case 'K': 
				case 'L': digit = 5; break;
				case '6': 
				case 'M': 
				case 'N': 
				case 'O': digit = 6; break;
				case '7': 
				case 'P': 
				case 'R':
				case 'S': digit = 7; break;
				case '8':
				case 'T':
				case 'U': 
				case 'V': digit = 8; break;
				case '9':
				case 'W': 
				case 'X': 
				case 'Y': digit = 9; break;
				default: digit = -1; break;
				};

				if (digit != -1)
					cur_num = cur_num * 10 + digit;							
			}

			map<int, int>::iterator TelefIter;			
			TelefIter = telnums.find(cur_num);

			if(telnums.end() == TelefIter)
			{
				telnums[cur_num] = 1;
			}			
			else
			{
				TelefIter->second++;
			}

		}

		bool hadsome = false;
		map<int, int>::const_iterator iter;
		for (iter = telnums.begin(); iter != telnums.end(); ++iter) 
		{
			if (iter->second > 1)
			{
				hadsome = true;
				char str[8];				
				sprintf(str, "%07d", iter->first);
				printf("%c%c%c-%c%c%c%c %d\n", 
						str[0], str[1], str[2], str[3], 
						str[4], str[5], str[6], iter->second);	        	
			}
		}

		if (!hadsome) printf("No duplicates.\n");

		printf("\n");
	}

	return 0;
}

Dani AI

Generated

A few focused things to try — most WA results on this problem come from subtle input/formatting differences between your local runs and the judge, not the digit-mapping itself.

  • Check blank-line handling. The judge input may omit or include blank lines in places your parsing assumes. Make your reader robust: skip empty lines before reading each dataset count, and only print the extra blank line between test cases (not after the last case). asked for inputs — try a variant with and without the blank lines to reproduce the failure.

  • Check letter case and unexpected characters. Convert chars with toupper() before mapping so lower-case test data won’t be ignored. Also confirm the judge never supplies Q or Z (some problem versions exclude those); if they do, decide whether to map or treat them as invalid per the statement.

  • Rework the input loop to avoid fragile sscanf-from-getline sequencing. Example pattern to adopt (replace the fragile parts of your current loop):

int T;
if (!(cin >> T)) return 0;
string line;
getline(cin, line); // consume rest of first line

for (int tc = 0; tc < T; ++tc) {
  // skip any blank lines that separate cases
  while (getline(cin, line) && line.empty());

  int N = stoi(line);    // now N is the count line
  for (int i = 0; i < N; ++i) {
    getline(cin, line);
    // normalize with toupper(line[k]), drop hyphens, build 7-digit string
  }

  // print results...
  if (tc + 1 < T) cout << '\n';   // blank line only between cases
}
  • Practical debugging tips: run the binary on the judge-like environment (Linux/g++), and add temporary debug output to stderr to dump each raw input line (show bytes) to catch stray CRs or invisible characters. If you still get WA, post one failing input excerpt; that’s what suggested and it will immediately show where parsing or formatting diverges.

Recommended Answers

All 3 Replies

It will help to know what inputs you used that caused the problem.

That's the thing about ACM. All inputs that I tested gave (IMHO) correct outputs. But the input the automatic controller used that gave wrong answer is unknown.

Post an example of the input data you tried. Maybe what the controlller did was feed your program bogus (invalid) data to see how your program would handle it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.