|
|
вернуться в форумHelp -- Don't understand why getting wrong answer Hi I have the solution which seems to work fine when I tested with sample input. But I get "wrong answer" judgement. Can any please look at see if I am making some obvious mistake. Many Thanks ------------------------------------------------- #include <iostream> #include <string> #include <vector> #include <iterator> #include <sstream> using namespace std; string code[] = {"oqz","ij","abc","def","gh","kl","mn","prs","tuv","wxy"}; int getArrMax(int *arr, int size){ int temp = 0; int val = 0; for(int i=0; i<size; i++){ if(arr[i] > val){ val = arr[i]; temp = i; } } return temp; } string match(string line, size_t start, size_t max_word_size, size_t& lastmatch, string* dictionary, int dict_size) { int* match = new int[dict_size]; for(int count=0;count<dict_size;count++) match[count]=0; for(size_t i=start;i < (start+max_word_size);i++) { const char c = line.at(i); int digit = atoi(&c); string rel_code = code[digit]; //find max val in match arry int MAX = match[getArrMax(match,dict_size)]; for(int k=0;k<dict_size;k++){ string dict_word = dictionary[k]; if((match[k] == MAX)&& (dict_word.length()>(i-start)) &&(rel_code.find(dict_word.at(i-start))!=string::npos)){ match[k]+=1; } } } int match_index = getArrMax(match,dict_size); string match_string = dictionary[match_index]; if(match[match_index] != match_string.length()) match_string = ""; lastmatch = start + match_string.length(); delete match; return match_string; } int main(){ vector<string> vec; string line; while(getline(cin,line) && line !="-1"){ std::stringstream trimmer; trimmer << line; trimmer >> line; cin.clear(); fflush(stdin); int dict_size; cin >> dict_size; string* dict = new string[dict_size]; size_t max_word_size = 0; for(int i=0;i<dict_size;i++) { cin.clear(); fflush(stdin); string dict_word; getline(cin,dict_word); std::stringstream trimmer; trimmer << dict_word; trimmer >> dict_word; dict[i] = dict_word; if( max_word_size < dict_word.length() ) max_word_size = dict_word.length(); } size_t lastmatch = 0; while(lastmatch >= 0 && lastmatch < (line.length()-1)){ size_t thismatch = lastmatch; string ss = match(line,lastmatch, max_word_size < (line.length()-lastmatch) ? max_word_size : (line.length()-lastmatch), lastmatch,dict,dict_size); if(thismatch==lastmatch) lastmatch = -1; else { vec.push_back(ss); if(lastmatch != line.length()) vec.push_back(" "); } } if(lastmatch == line.length()) vec.push_back("\n"); else vec.push_back("No solution.\n"); delete[] dict; } copy(vec.begin(),vec.end(),ostream_iterator<string>(cout,"")); } |
|
|