db58c713f6 2012-02-24 kinaba: #include <iostream> db58c713f6 2012-02-24 kinaba: #include <sstream> db58c713f6 2012-02-24 kinaba: #include <iomanip> db58c713f6 2012-02-24 kinaba: #include <vector> db58c713f6 2012-02-24 kinaba: #include <string> db58c713f6 2012-02-24 kinaba: #include <map> db58c713f6 2012-02-24 kinaba: #include <set> db58c713f6 2012-02-24 kinaba: #include <algorithm> db58c713f6 2012-02-24 kinaba: #include <numeric> db58c713f6 2012-02-24 kinaba: #include <iterator> db58c713f6 2012-02-24 kinaba: #include <functional> db58c713f6 2012-02-24 kinaba: #include <complex> db58c713f6 2012-02-24 kinaba: #include <queue> db58c713f6 2012-02-24 kinaba: #include <stack> db58c713f6 2012-02-24 kinaba: #include <cmath> db58c713f6 2012-02-24 kinaba: #include <cassert> db58c713f6 2012-02-24 kinaba: #include <cstring> db58c713f6 2012-02-24 kinaba: #ifdef __GNUC__ db58c713f6 2012-02-24 kinaba: #include <ext/hash_map> db58c713f6 2012-02-24 kinaba: #define unordered_map __gnu_cxx::hash_map db58c713f6 2012-02-24 kinaba: #else db58c713f6 2012-02-24 kinaba: #include <unordered_map> db58c713f6 2012-02-24 kinaba: #endif db58c713f6 2012-02-24 kinaba: using namespace std; db58c713f6 2012-02-24 kinaba: typedef long long LL; db58c713f6 2012-02-24 kinaba: typedef complex<double> CMP; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: class EllysString { public: db58c713f6 2012-02-24 kinaba: int theMin(vector <string> s, vector <string> t) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: string S = accumulate(s.begin(), s.end(), string("")); db58c713f6 2012-02-24 kinaba: string T = accumulate(t.begin(), t.end(), string("")); db58c713f6 2012-02-24 kinaba: int N = S.size(); db58c713f6 2012-02-24 kinaba: int total = 0; db58c713f6 2012-02-24 kinaba: for(int i=0; i<N; ) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: if( S[i] == T[i] ) { db58c713f6 2012-02-24 kinaba: ++i; db58c713f6 2012-02-24 kinaba: continue; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: int rot_k = -1; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: // abcdef db58c713f6 2012-02-24 kinaba: // bcdefa db58c713f6 2012-02-24 kinaba: for(int k=i+1; k<N; ++k) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: if( S[k] != T[k-1] ) db58c713f6 2012-02-24 kinaba: break; db58c713f6 2012-02-24 kinaba: if( T[k] == S[i] ) db58c713f6 2012-02-24 kinaba: rot_k = max(rot_k, k+1); db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: // bcdefa db58c713f6 2012-02-24 kinaba: // abcdef db58c713f6 2012-02-24 kinaba: for(int k=i+1; k<N; ++k) db58c713f6 2012-02-24 kinaba: { db58c713f6 2012-02-24 kinaba: if( T[k] != S[k-1] ) db58c713f6 2012-02-24 kinaba: break; db58c713f6 2012-02-24 kinaba: if( S[k] == T[i] ) db58c713f6 2012-02-24 kinaba: rot_k = max(rot_k, k+1); db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: if( rot_k == -1 ) { db58c713f6 2012-02-24 kinaba: total += 1; db58c713f6 2012-02-24 kinaba: ++i; db58c713f6 2012-02-24 kinaba: continue; db58c713f6 2012-02-24 kinaba: } else { db58c713f6 2012-02-24 kinaba: total += rot_k - i - 1; db58c713f6 2012-02-24 kinaba: i = rot_k; db58c713f6 2012-02-24 kinaba: continue; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: return total; db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: }; db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: // BEGIN CUT HERE db58c713f6 2012-02-24 kinaba: #include <ctime> db58c713f6 2012-02-24 kinaba: double start_time; string timer() db58c713f6 2012-02-24 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } db58c713f6 2012-02-24 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) db58c713f6 2012-02-24 kinaba: { os << "{ "; db58c713f6 2012-02-24 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) db58c713f6 2012-02-24 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } db58c713f6 2012-02-24 kinaba: void verify_case(const int& Expected, const int& Received) { db58c713f6 2012-02-24 kinaba: bool ok = (Expected == Received); db58c713f6 2012-02-24 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; db58c713f6 2012-02-24 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } db58c713f6 2012-02-24 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); db58c713f6 2012-02-24 kinaba: #define END verify_case(_, EllysString().theMin(s, t));} db58c713f6 2012-02-24 kinaba: int main(){ db58c713f6 2012-02-24 kinaba: db58c713f6 2012-02-24 kinaba: CASE(0) db58c713f6 2012-02-24 kinaba: string s_[] = {"usagi"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"unagi"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 1; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(1) db58c713f6 2012-02-24 kinaba: string s_[] = {"unagi"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"nugai"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 2; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(2) db58c713f6 2012-02-24 kinaba: string s_[] = {"ellys", "string"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"e", "ll", "ysst", "ring"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 0; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(3) db58c713f6 2012-02-24 kinaba: string s_[] = {"fox"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"xfo"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 2; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(4) db58c713f6 2012-02-24 kinaba: string s_[] = {"salmon"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"camlon"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 2; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(5) db58c713f6 2012-02-24 kinaba: string s_[] = {"boajxuidva"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"jcayduvida"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 6; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(6) db58c713f6 2012-02-24 kinaba: string s_[] = {"vykdnujyezbcbmnatipqfuxqmgkvtn"}; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = {"qokbqyujeqcbwsinatupqfoegmvkdz"}; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = 22; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: /* db58c713f6 2012-02-24 kinaba: CASE(7) db58c713f6 2012-02-24 kinaba: string s_[] = ; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = ; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = ; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: CASE(8) db58c713f6 2012-02-24 kinaba: string s_[] = ; db58c713f6 2012-02-24 kinaba: vector <string> s(s_, s_+sizeof(s_)/sizeof(*s_)); db58c713f6 2012-02-24 kinaba: string t_[] = ; db58c713f6 2012-02-24 kinaba: vector <string> t(t_, t_+sizeof(t_)/sizeof(*t_)); db58c713f6 2012-02-24 kinaba: int _ = ; db58c713f6 2012-02-24 kinaba: END db58c713f6 2012-02-24 kinaba: */ db58c713f6 2012-02-24 kinaba: } db58c713f6 2012-02-24 kinaba: // END CUT HERE