7e18a763ef 2013-01-29 kinaba: #include <iostream> 7e18a763ef 2013-01-29 kinaba: #include <sstream> 7e18a763ef 2013-01-29 kinaba: #include <iomanip> 7e18a763ef 2013-01-29 kinaba: #include <vector> 7e18a763ef 2013-01-29 kinaba: #include <string> 7e18a763ef 2013-01-29 kinaba: #include <map> 7e18a763ef 2013-01-29 kinaba: #include <set> 7e18a763ef 2013-01-29 kinaba: #include <algorithm> 7e18a763ef 2013-01-29 kinaba: #include <numeric> 7e18a763ef 2013-01-29 kinaba: #include <iterator> 7e18a763ef 2013-01-29 kinaba: #include <functional> 7e18a763ef 2013-01-29 kinaba: #include <complex> 7e18a763ef 2013-01-29 kinaba: #include <queue> 7e18a763ef 2013-01-29 kinaba: #include <stack> 7e18a763ef 2013-01-29 kinaba: #include <cmath> 7e18a763ef 2013-01-29 kinaba: #include <cassert> 7e18a763ef 2013-01-29 kinaba: using namespace std; 7e18a763ef 2013-01-29 kinaba: typedef long long LL; 7e18a763ef 2013-01-29 kinaba: typedef long double LD; 7e18a763ef 2013-01-29 kinaba: typedef complex<LD> CMP; 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: class StringGame { public: 7e18a763ef 2013-01-29 kinaba: vector <int> getWinningStrings(vector <string> S) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: vector< vector<int> > cb; 7e18a763ef 2013-01-29 kinaba: for(int i=0; i<S.size(); ++i) 7e18a763ef 2013-01-29 kinaba: cb.push_back(as_cb(S[i])); 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: vector<int> result; 7e18a763ef 2013-01-29 kinaba: for(int i=0; i<cb.size(); ++i) 7e18a763ef 2013-01-29 kinaba: if( can_win(cb, i) ) 7e18a763ef 2013-01-29 kinaba: result.push_back(i); 7e18a763ef 2013-01-29 kinaba: return result; 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: vector<int> as_cb(const string& s) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: vector<int> cb(26); 7e18a763ef 2013-01-29 kinaba: for(int i=0; i<s.size(); ++i) 7e18a763ef 2013-01-29 kinaba: cb[s[i]-'a']++; 7e18a763ef 2013-01-29 kinaba: return cb; 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: bool can_win(const vector< vector<int> >& cb, int I) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: vector<int> enemy; 7e18a763ef 2013-01-29 kinaba: for(int i=0; i<cb.size(); ++i) 7e18a763ef 2013-01-29 kinaba: if(i != I) 7e18a763ef 2013-01-29 kinaba: enemy.push_back(i); 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: while(!enemy.empty()) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: bool update = false; 7e18a763ef 2013-01-29 kinaba: for(int c=0; c<26; ++c) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: vector<int> win, draw; 7e18a763ef 2013-01-29 kinaba: for(int i=0; i<enemy.size(); ++i) 7e18a763ef 2013-01-29 kinaba: { 7e18a763ef 2013-01-29 kinaba: int e = enemy[i]; 7e18a763ef 2013-01-29 kinaba: if(cb[e][c] < cb[I][c]) 7e18a763ef 2013-01-29 kinaba: win.push_back(e); 7e18a763ef 2013-01-29 kinaba: else if(cb[e][c] == cb[I][c]) 7e18a763ef 2013-01-29 kinaba: draw.push_back(e); 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: if(!win.empty() && win.size() + draw.size() == enemy.size()) { 7e18a763ef 2013-01-29 kinaba: enemy = draw; 7e18a763ef 2013-01-29 kinaba: update = true; 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: if(!update) 7e18a763ef 2013-01-29 kinaba: return false; 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: return true; 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: }; 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: // BEGIN CUT HERE 7e18a763ef 2013-01-29 kinaba: #include <ctime> 7e18a763ef 2013-01-29 kinaba: double start_time; string timer() 7e18a763ef 2013-01-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 7e18a763ef 2013-01-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 7e18a763ef 2013-01-29 kinaba: { os << "{ "; 7e18a763ef 2013-01-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 7e18a763ef 2013-01-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 7e18a763ef 2013-01-29 kinaba: void verify_case(const vector <int>& Expected, const vector <int>& Received) { 7e18a763ef 2013-01-29 kinaba: bool ok = (Expected == Received); 7e18a763ef 2013-01-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 7e18a763ef 2013-01-29 kinaba: cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } } 7e18a763ef 2013-01-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 7e18a763ef 2013-01-29 kinaba: #define END verify_case(_, StringGame().getWinningStrings(S));} 7e18a763ef 2013-01-29 kinaba: int main(){ 7e18a763ef 2013-01-29 kinaba: 7e18a763ef 2013-01-29 kinaba: CASE(0) 7e18a763ef 2013-01-29 kinaba: string S_[] = {"a", "b", "c", "d"}; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: int __[] = {0, 1, 2, 3 }; 7e18a763ef 2013-01-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: CASE(1) 7e18a763ef 2013-01-29 kinaba: string S_[] = {"aabbcc", "aaabbb", "aaaccc"}; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: int __[] = {1, 2 }; 7e18a763ef 2013-01-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: CASE(2) 7e18a763ef 2013-01-29 kinaba: string S_[] = {"ab", "ba"}; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: vector <int> _; 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: CASE(3) 7e18a763ef 2013-01-29 kinaba: string S_[] = {"xaocxsss", "oooxsoas", "xaooosss", "xaocssss", "coxaosxx"}; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: int __[] = {1, 3, 4 }; 7e18a763ef 2013-01-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: CASE(4) 7e18a763ef 2013-01-29 kinaba: string S_[] = {"aaabbc", "aabbcc", "aaaabc"}; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: int __[] = {0, 1, 2}; 7e18a763ef 2013-01-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: /* 7e18a763ef 2013-01-29 kinaba: CASE(5) 7e18a763ef 2013-01-29 kinaba: string S_[] = ; 7e18a763ef 2013-01-29 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 7e18a763ef 2013-01-29 kinaba: int __[] = ; 7e18a763ef 2013-01-29 kinaba: vector <int> _(__, __+sizeof(__)/sizeof(*__)); 7e18a763ef 2013-01-29 kinaba: END 7e18a763ef 2013-01-29 kinaba: */ 7e18a763ef 2013-01-29 kinaba: } 7e18a763ef 2013-01-29 kinaba: // END CUT HERE