http://acm.pku.edu.cn/JudgeOnline/problem?id=1219
Код:
#include <vector> #include <iostream> #include <algorithm> #include <iterator> #include <string> #include <sstream> using namespace std; bool DEBUG=false; const int MAX_GUESSES=6; struct game { string word; vector<string> guesses; }; istream& operator>>(istream& in, game& g) { string line; getline(cin, line); { istringstream in(line); in >> g.word; } if(g.word=="LINGO") return in; while(true) { getline(cin, line); if(line=="") return in; string guess; { istringstream in(line); in >> guess; } g.guesses.push_back(guess); } } bool guess_valid(string guess) { if(guess.size() != 5) return false; for(int i=0; i<5; ++i) { if(!isupper(guess[i])) return false; } if(DEBUG) cout << guess << " is valid.\n"; return true; } string eval_guess(string guess, string word) { string result = "....."; for(int i=0; i<5; ++i) { if(guess[i]==word[i]) result[i] = guess[i]; } if(DEBUG) cout << "Correct letters: " << result << endl; for(int i=0; i<5; ++i) { if(guess[i] != result[i]) { if(count(word.begin(), word.end(), guess[i]) - count(result.begin(), result.end(), guess[i]) - count(result.begin(), result.end(), tolower(guess[i])) >0) { result[i] = tolower(guess[i]); } } } if(DEBUG) cout << "All letters: " << result << endl; return result; } int main() { { string trash; getline(cin, trash); } while(true) { game g; cin >> g; if(g.word=="LINGO") break; cout << endl; string prev_result = g.word[0] + string("...."); cout << prev_result << endl; for(unsigned int i=0; i<g.guesses.size(); ++i) { if(guess_valid(g.guesses[i])) { prev_result = eval_guess(g.guesses[i], g.word); } if(prev_result == g.word) { cout << prev_result << endl; break; } if(i+1==MAX_GUESSES) break; cout << prev_result << endl; } if(prev_result != g.word) { for(int i=0; i<5; ++i) cout << char(tolower(g.word[i])); cout << endl; } } }