c0b96abfe5 2013-03-15 kinaba: #include <iostream> c0b96abfe5 2013-03-15 kinaba: #include <sstream> c0b96abfe5 2013-03-15 kinaba: #include <iomanip> c0b96abfe5 2013-03-15 kinaba: #include <vector> c0b96abfe5 2013-03-15 kinaba: #include <string> c0b96abfe5 2013-03-15 kinaba: #include <map> c0b96abfe5 2013-03-15 kinaba: #include <set> c0b96abfe5 2013-03-15 kinaba: #include <algorithm> c0b96abfe5 2013-03-15 kinaba: #include <numeric> c0b96abfe5 2013-03-15 kinaba: #include <iterator> c0b96abfe5 2013-03-15 kinaba: #include <functional> c0b96abfe5 2013-03-15 kinaba: #include <complex> c0b96abfe5 2013-03-15 kinaba: #include <queue> c0b96abfe5 2013-03-15 kinaba: #include <stack> c0b96abfe5 2013-03-15 kinaba: #include <cmath> c0b96abfe5 2013-03-15 kinaba: #include <cassert> c0b96abfe5 2013-03-15 kinaba: using namespace std; c0b96abfe5 2013-03-15 kinaba: typedef long long LL; c0b96abfe5 2013-03-15 kinaba: typedef long double LD; c0b96abfe5 2013-03-15 kinaba: typedef complex<LD> CMP; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: class EllysBulls { public: c0b96abfe5 2013-03-15 kinaba: string getNumber(vector <string> guesses, vector <int> bulls) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: vector<int> pTen(1,1); c0b96abfe5 2013-03-15 kinaba: for(int i=0; i<10; ++i) c0b96abfe5 2013-03-15 kinaba: pTen.push_back(pTen.back()*10); c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: int Q = guesses.size(); c0b96abfe5 2013-03-15 kinaba: int N = guesses[0].size(); c0b96abfe5 2013-03-15 kinaba: int B = min(N, 4); c0b96abfe5 2013-03-15 kinaba: int F = N - B; c0b96abfe5 2013-03-15 kinaba: map<string, vector<int> > back; c0b96abfe5 2013-03-15 kinaba: for(int b=0; b<pTen[B]; ++b) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: string sig; c0b96abfe5 2013-03-15 kinaba: for(int i=0; i<Q; ++i) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: int s = bulls[i]; c0b96abfe5 2013-03-15 kinaba: for(int k=0; k<B; ++k) c0b96abfe5 2013-03-15 kinaba: if( (guesses[i][N-1-k]-'0') == b/pTen[k]%10 ) c0b96abfe5 2013-03-15 kinaba: --s; c0b96abfe5 2013-03-15 kinaba: if(s>=0) c0b96abfe5 2013-03-15 kinaba: sig += char('0'+s); c0b96abfe5 2013-03-15 kinaba: else c0b96abfe5 2013-03-15 kinaba: goto next; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: back[sig].push_back(b); c0b96abfe5 2013-03-15 kinaba: next:; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: string cand = ""; c0b96abfe5 2013-03-15 kinaba: for(int f=0; f<pTen[F]; ++f) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: string sig; c0b96abfe5 2013-03-15 kinaba: map<string, vector<int> >::iterator it; c0b96abfe5 2013-03-15 kinaba: for(int i=0; i<Q; ++i) c0b96abfe5 2013-03-15 kinaba: { c0b96abfe5 2013-03-15 kinaba: int s = 0; c0b96abfe5 2013-03-15 kinaba: for(int k=0; k<F; ++k) c0b96abfe5 2013-03-15 kinaba: if( (guesses[i][F-1-k]-'0') == f/pTen[k]%10 ) c0b96abfe5 2013-03-15 kinaba: ++s; c0b96abfe5 2013-03-15 kinaba: if(s<=bulls[i]) c0b96abfe5 2013-03-15 kinaba: sig += char('0'+s); c0b96abfe5 2013-03-15 kinaba: else c0b96abfe5 2013-03-15 kinaba: goto next2; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: it = back.find(sig); c0b96abfe5 2013-03-15 kinaba: if( it != back.end() ) { c0b96abfe5 2013-03-15 kinaba: for(int j=0; j<it->second.size(); ++j) { c0b96abfe5 2013-03-15 kinaba: if(!cand.empty()) c0b96abfe5 2013-03-15 kinaba: return "Ambiguity"; c0b96abfe5 2013-03-15 kinaba: for(int k=0; k<F; ++k) c0b96abfe5 2013-03-15 kinaba: cand += char('0'+f/pTen[F-1-k]%10); c0b96abfe5 2013-03-15 kinaba: for(int k=0; k<B; ++k) c0b96abfe5 2013-03-15 kinaba: cand += char('0'+it->second[j]/pTen[B-1-k]%10); c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: next2:; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: if(cand.empty()) c0b96abfe5 2013-03-15 kinaba: return "Liar"; c0b96abfe5 2013-03-15 kinaba: return cand; c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: }; c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: // BEGIN CUT HERE c0b96abfe5 2013-03-15 kinaba: #include <ctime> c0b96abfe5 2013-03-15 kinaba: double start_time; string timer() c0b96abfe5 2013-03-15 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c0b96abfe5 2013-03-15 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c0b96abfe5 2013-03-15 kinaba: { os << "{ "; c0b96abfe5 2013-03-15 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c0b96abfe5 2013-03-15 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c0b96abfe5 2013-03-15 kinaba: void verify_case(const string& Expected, const string& Received) { c0b96abfe5 2013-03-15 kinaba: bool ok = (Expected == Received); c0b96abfe5 2013-03-15 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c0b96abfe5 2013-03-15 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c0b96abfe5 2013-03-15 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c0b96abfe5 2013-03-15 kinaba: #define END verify_case(_, EllysBulls().getNumber(guesses, bulls));} c0b96abfe5 2013-03-15 kinaba: int main(){ c0b96abfe5 2013-03-15 kinaba: c0b96abfe5 2013-03-15 kinaba: CASE(0) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"1234", "4321", "1111", "2222", "3333", "4444", "5555", "6666", "7777", "8888", "9999"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {2, 1, 1, 0, 2, 0, 0, 0, 1, 0, 0}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "1337"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(1) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"0000", "1111", "2222"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {2, 2, 2}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "Liar"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(2) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"666666", "666677", "777777", "999999"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {2, 3, 1, 0}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "Ambiguity"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(3) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"000", "987", "654", "321", "100", "010"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {2, 1, 0, 0, 1, 1}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "007"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(4) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"28", "92", "70", "30", "67", "63", "06", "65", c0b96abfe5 2013-03-15 kinaba: "11", "06", "88", "48", "09", "65", "48", "08"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "54"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(5) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = {"0294884", "1711527", "2362216", "7666148", "7295642", c0b96abfe5 2013-03-15 kinaba: "4166623", "1166638", "2767693", "8650248", "2486509", c0b96abfe5 2013-03-15 kinaba: "6138934", "4018642", "6236742", "2961643", "8407361", c0b96abfe5 2013-03-15 kinaba: "2097376", "6575410", "6071777", "3569948", "2606380"}; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = {1, 0, 1, 3, 4, 4, 3, 2, 1, 1, 0, 4, 4, 3, 0, 0, 0, 0, 2, 1}; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = "4266642"; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: /* c0b96abfe5 2013-03-15 kinaba: CASE(6) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = ; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = ; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = ; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: CASE(7) c0b96abfe5 2013-03-15 kinaba: string guesses_[] = ; c0b96abfe5 2013-03-15 kinaba: vector <string> guesses(guesses_, guesses_+sizeof(guesses_)/sizeof(*guesses_)); c0b96abfe5 2013-03-15 kinaba: int bulls_[] = ; c0b96abfe5 2013-03-15 kinaba: vector <int> bulls(bulls_, bulls_+sizeof(bulls_)/sizeof(*bulls_)); c0b96abfe5 2013-03-15 kinaba: string _ = ; c0b96abfe5 2013-03-15 kinaba: END c0b96abfe5 2013-03-15 kinaba: */ c0b96abfe5 2013-03-15 kinaba: } c0b96abfe5 2013-03-15 kinaba: // END CUT HERE