7764536bd3 2013-11-29 kinaba: #include <iostream> 7764536bd3 2013-11-29 kinaba: #include <sstream> 7764536bd3 2013-11-29 kinaba: #include <iomanip> 7764536bd3 2013-11-29 kinaba: #include <vector> 7764536bd3 2013-11-29 kinaba: #include <string> 7764536bd3 2013-11-29 kinaba: #include <map> 7764536bd3 2013-11-29 kinaba: #include <set> 7764536bd3 2013-11-29 kinaba: #include <algorithm> 7764536bd3 2013-11-29 kinaba: #include <numeric> 7764536bd3 2013-11-29 kinaba: #include <iterator> 7764536bd3 2013-11-29 kinaba: #include <functional> 7764536bd3 2013-11-29 kinaba: #include <complex> 7764536bd3 2013-11-29 kinaba: #include <queue> 7764536bd3 2013-11-29 kinaba: #include <stack> 7764536bd3 2013-11-29 kinaba: #include <cmath> 7764536bd3 2013-11-29 kinaba: #include <cassert> 7764536bd3 2013-11-29 kinaba: #include <tuple> 7764536bd3 2013-11-29 kinaba: using namespace std; 7764536bd3 2013-11-29 kinaba: typedef long long LL; 7764536bd3 2013-11-29 kinaba: typedef complex<double> CMP; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: class LittleElephantAndString { public: 7764536bd3 2013-11-29 kinaba: int getNumber(string A, string B) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: string AA = A, BB = B; 7764536bd3 2013-11-29 kinaba: sort(AA.begin(), AA.end()); 7764536bd3 2013-11-29 kinaba: sort(BB.begin(), BB.end()); 7764536bd3 2013-11-29 kinaba: if(AA != BB) 7764536bd3 2013-11-29 kinaba: return -1; 7764536bd3 2013-11-29 kinaba: for(int n=0; n<B.size(); ++n) 7764536bd3 2013-11-29 kinaba: if(sup_sub(A, B.substr(n))) 7764536bd3 2013-11-29 kinaba: return n; 7764536bd3 2013-11-29 kinaba: assert(false); 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: bool sup_sub(const string& A, const string& B) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: for(int a=0, b=0; b<B.size(); ++a,++b) 7764536bd3 2013-11-29 kinaba: { 7764536bd3 2013-11-29 kinaba: while(a<A.size() && A[a]!=B[b]) 7764536bd3 2013-11-29 kinaba: ++a; 7764536bd3 2013-11-29 kinaba: if(a == A.size()) 7764536bd3 2013-11-29 kinaba: return false; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: return true; 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: }; 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: // BEGIN CUT HERE 7764536bd3 2013-11-29 kinaba: #include <ctime> 7764536bd3 2013-11-29 kinaba: double start_time; string timer() 7764536bd3 2013-11-29 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 7764536bd3 2013-11-29 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 7764536bd3 2013-11-29 kinaba: { os << "{ "; 7764536bd3 2013-11-29 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 7764536bd3 2013-11-29 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 7764536bd3 2013-11-29 kinaba: void verify_case(const int& Expected, const int& Received) { 7764536bd3 2013-11-29 kinaba: bool ok = (Expected == Received); 7764536bd3 2013-11-29 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 7764536bd3 2013-11-29 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 7764536bd3 2013-11-29 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 7764536bd3 2013-11-29 kinaba: #define END verify_case(_, LittleElephantAndString().getNumber(A, B));} 7764536bd3 2013-11-29 kinaba: int main(){ 7764536bd3 2013-11-29 kinaba: 7764536bd3 2013-11-29 kinaba: CASE(0) 7764536bd3 2013-11-29 kinaba: string A = "ABC"; 7764536bd3 2013-11-29 kinaba: string B = "CBA"; 7764536bd3 2013-11-29 kinaba: int _ = 2; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(1) 7764536bd3 2013-11-29 kinaba: string A = "A"; 7764536bd3 2013-11-29 kinaba: string B = "B"; 7764536bd3 2013-11-29 kinaba: int _ = -1; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(2) 7764536bd3 2013-11-29 kinaba: string A = "AAABBB"; 7764536bd3 2013-11-29 kinaba: string B = "BBBAAA"; 7764536bd3 2013-11-29 kinaba: int _ = 3; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(3) 7764536bd3 2013-11-29 kinaba: string A = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 7764536bd3 2013-11-29 kinaba: string B = "ZYXWVUTSRQPONMLKJIHGFEDCBA"; 7764536bd3 2013-11-29 kinaba: int _ = 25; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(4) 7764536bd3 2013-11-29 kinaba: string A = "A"; 7764536bd3 2013-11-29 kinaba: string B = "A"; 7764536bd3 2013-11-29 kinaba: int _ = 0; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(5) 7764536bd3 2013-11-29 kinaba: string A = "DCABA"; 7764536bd3 2013-11-29 kinaba: string B = "DACBA"; 7764536bd3 2013-11-29 kinaba: int _ = 2; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: /* 7764536bd3 2013-11-29 kinaba: CASE(6) 7764536bd3 2013-11-29 kinaba: string A = ; 7764536bd3 2013-11-29 kinaba: string B = ; 7764536bd3 2013-11-29 kinaba: int _ = ; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: CASE(7) 7764536bd3 2013-11-29 kinaba: string A = ; 7764536bd3 2013-11-29 kinaba: string B = ; 7764536bd3 2013-11-29 kinaba: int _ = ; 7764536bd3 2013-11-29 kinaba: END 7764536bd3 2013-11-29 kinaba: */ 7764536bd3 2013-11-29 kinaba: } 7764536bd3 2013-11-29 kinaba: // END CUT HERE