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 RepeatedPatterns { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: long long findZeroSegment(string P1, string P2, string zeroCount) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL zc; stringstream(zeroCount)>>zc; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool allz1 = (P1.find('1') == string::npos); 4fd800b3a8 2011-02-23 kinaba: bool allz2 = (P2.find('1') == string::npos); 4fd800b3a8 2011-02-23 kinaba: if( allz1 && allz2 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( allz1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL z1 = P1.size()*1000000; 4fd800b3a8 2011-02-23 kinaba: if( zc <= z1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL a2 = 0; while( P2[a2]=='0' ) ++a2; 4fd800b3a8 2011-02-23 kinaba: LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; 4fd800b3a8 2011-02-23 kinaba: LL c2=0, i2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<P2.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( P2[i]=='0' ) { 4fd800b3a8 2011-02-23 kinaba: size_t ii = P2.find('1',i); 4fd800b3a8 2011-02-23 kinaba: if(ii==string::npos) ii=P2.size(); 4fd800b3a8 2011-02-23 kinaba: if( ii-i > c2 ) c2=ii-i, i2=i; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( zc <= z1+a2 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: if( zc <= c2 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 + i2; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b2+z1+a2 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 + P2.size() - b2; 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else if( allz2 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL a1 = 0; while( P1[a1]=='0' ) ++a1; 4fd800b3a8 2011-02-23 kinaba: LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; 4fd800b3a8 2011-02-23 kinaba: LL c1=0, i1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<P1.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( P1[i]=='0' ) { 4fd800b3a8 2011-02-23 kinaba: size_t ii = P1.find('1',i); 4fd800b3a8 2011-02-23 kinaba: if(ii==string::npos) ii=P1.size(); 4fd800b3a8 2011-02-23 kinaba: if( ii-i > c1 ) c1=ii-i, i1=i; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( zc <= a1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: if( zc <= c1 ) 4fd800b3a8 2011-02-23 kinaba: return i1; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b1+a1 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size() - b1; 4fd800b3a8 2011-02-23 kinaba: // zc <= b1 + k*P2.size() + a1; 4fd800b3a8 2011-02-23 kinaba: LL k = (zc-b1-a1) / P2.size() + ((zc-b1-a1)%P2.size() ? 1 : 0); 4fd800b3a8 2011-02-23 kinaba: LL idx = P1.size()*1000000*k + (k-1)*k/2*P2.size() - b1; 4fd800b3a8 2011-02-23 kinaba: return 0<=idx && idx+zc<=10000000000000000LL ? idx : -1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL a1 = 0; while( P1[a1]=='0' ) ++a1; 4fd800b3a8 2011-02-23 kinaba: LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1; 4fd800b3a8 2011-02-23 kinaba: LL c1=0, i1; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<P1.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( P1[i]=='0' ) { 4fd800b3a8 2011-02-23 kinaba: size_t ii = P1.find('1',i); 4fd800b3a8 2011-02-23 kinaba: if(ii==string::npos) ii=P1.size(); 4fd800b3a8 2011-02-23 kinaba: if( ii-i > c1 ) c1=ii-i, i1=i; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL a2 = 0; while( P2[a2]=='0' ) ++a2; 4fd800b3a8 2011-02-23 kinaba: LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2; 4fd800b3a8 2011-02-23 kinaba: LL c2=0, i2; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<P2.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: if( P2[i]=='0' ) { 4fd800b3a8 2011-02-23 kinaba: size_t ii = P2.find('1',i); 4fd800b3a8 2011-02-23 kinaba: if(ii==string::npos) ii=P2.size(); 4fd800b3a8 2011-02-23 kinaba: if( ii-i > c2 ) c2=ii-i, i2=i; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( zc <= a1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: if( zc <= c1 ) 4fd800b3a8 2011-02-23 kinaba: return i1; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b1+a1 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size() - b1; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b1+a2 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 - b1; 4fd800b3a8 2011-02-23 kinaba: if( zc <= c2 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 + i2; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b2+a1 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 + P2.size() - b2; 4fd800b3a8 2011-02-23 kinaba: if( zc <= b2+a2 ) 4fd800b3a8 2011-02-23 kinaba: return P1.size()*1000000 + P2.size() + P1.size()*1000000 + P2.size() - b2; 4fd800b3a8 2011-02-23 kinaba: return -1; 4fd800b3a8 2011-02-23 kinaba: } 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 long long &Expected, const long long &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 P1 = "111010100001010"; 4fd800b3a8 2011-02-23 kinaba: string P2 = "010000001000"; 4fd800b3a8 2011-02-23 kinaba: string zeroCount = "3"; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 7LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: string P1 = "1101010010"; 4fd800b3a8 2011-02-23 kinaba: string P2 = "0001000"; 4fd800b3a8 2011-02-23 kinaba: string zeroCount = "3"; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 9999999LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: string P1 = "1101010010"; 4fd800b3a8 2011-02-23 kinaba: string P2 = "0001000"; 4fd800b3a8 2011-02-23 kinaba: string zeroCount = "5"; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = 20000011LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: string P1 = "10101010"; 4fd800b3a8 2011-02-23 kinaba: string P2 = "101010101010"; 4fd800b3a8 2011-02-23 kinaba: string zeroCount = "9876543219876"; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = -1LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: string P1 = "11111111111111111111111111"; 4fd800b3a8 2011-02-23 kinaba: string P2 = "0"; 4fd800b3a8 2011-02-23 kinaba: string zeroCount = "9876543219876"; 4fd800b3a8 2011-02-23 kinaba: long long RetVal = -1LL; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); } 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: