4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class MagicWords { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int count(vector <string> S, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int cnt = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int idx[] = {0,1,2,3,4,5,6,7}; 4fd800b3a8 2011-02-23 kinaba: do { 4fd800b3a8 2011-02-23 kinaba: string s; for(int i=0; i<S.size(); ++i) s += S[idx[i]]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( s.size()%K==0 && rep_of(s, s.size()/K) ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int n=1; n<s.size()/K; ++n) 4fd800b3a8 2011-02-23 kinaba: if( s.size()%n==0 && rep_of(s, n) ) 4fd800b3a8 2011-02-23 kinaba: goto fail; 4fd800b3a8 2011-02-23 kinaba: ++cnt; 4fd800b3a8 2011-02-23 kinaba: fail:; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } while( next_permutation(idx+0, idx+S.size()) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: return cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool rep_of( string& s, int n ) { 4fd800b3a8 2011-02-23 kinaba: return s == s.substr(n, s.size()-n) + s.substr(0, n); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"CAD","ABRA","ABRA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"AB","RAAB","RA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 3; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"AA","AA","AAA","A"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"AA","AA","AAA","A","AAA","AAAA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 15; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 720; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"ABC","AB","ABC","CA"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"A","B","C","A","B","C"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 672; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: string S_[] = {"AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAA", 4fd800b3a8 2011-02-23 kinaba: "AAAAAAAAAAAAAAAAAAAB"}; 4fd800b3a8 2011-02-23 kinaba: vector <string> S(S_, S_+sizeof(S_)/sizeof(*S_)); 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 40320; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, MagicWords().count(S, K)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: