Artifact Content
Not logged in

Artifact 42a89a8a845fafd629a4fe760c253df538124dfe


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

class RepeatedPatterns {
public:
	long long findZeroSegment(string P1, string P2, string zeroCount) 
	{
		LL zc; stringstream(zeroCount)>>zc;

		bool allz1 = (P1.find('1') == string::npos);
		bool allz2 = (P2.find('1') == string::npos);
		if( allz1 && allz2 )
			return 0;

		if( allz1 )
		{
			LL z1 = P1.size()*1000000;
			if( zc <= z1 )
				return 0;

			LL a2 = 0; while( P2[a2]=='0' )             ++a2;
			LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2;
			LL c2=0, i2;
			for(int i=0; i<P2.size(); ++i)
				if( P2[i]=='0' ) {
					size_t ii = P2.find('1',i);
					if(ii==string::npos) ii=P2.size();
					if( ii-i > c2 ) c2=ii-i, i2=i;
				}

			if( zc <= z1+a2 )
				return 0;
			if( zc <= c2 )
				return P1.size()*1000000 + i2;
			if( zc <= b2+z1+a2 )
				return P1.size()*1000000 + P2.size() - b2;
			return -1;
		}
		else if( allz2 )
		{
			LL a1 = 0; while( P1[a1]=='0' )             ++a1;
			LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1;
			LL c1=0, i1;
			for(int i=0; i<P1.size(); ++i)
				if( P1[i]=='0' ) {
					size_t ii = P1.find('1',i);
					if(ii==string::npos) ii=P1.size();
					if( ii-i > c1 ) c1=ii-i, i1=i;
				}
			if( zc <= a1 )
				return 0;
			if( zc <= c1 )
				return i1;
			if( zc <= b1+a1 )
				return P1.size() - b1;
			// zc <= b1 + k*P2.size() + a1;
			LL k = (zc-b1-a1) / P2.size() + ((zc-b1-a1)%P2.size() ? 1 : 0);
			LL idx = P1.size()*1000000*k + (k-1)*k/2*P2.size() - b1;
			return 0<=idx && idx+zc<=10000000000000000LL ? idx : -1;
		}
		else
		{
			LL a1 = 0; while( P1[a1]=='0' )             ++a1;
			LL b1 = 0; while( P1[P1.size()-1-b1]=='0' ) ++b1;
			LL c1=0, i1;
			for(int i=0; i<P1.size(); ++i)
				if( P1[i]=='0' ) {
					size_t ii = P1.find('1',i);
					if(ii==string::npos) ii=P1.size();
					if( ii-i > c1 ) c1=ii-i, i1=i;
				}
			LL a2 = 0; while( P2[a2]=='0' )             ++a2;
			LL b2 = 0; while( P2[P2.size()-1-b2]=='0' ) ++b2;
			LL c2=0, i2;
			for(int i=0; i<P2.size(); ++i)
				if( P2[i]=='0' ) {
					size_t ii = P2.find('1',i);
					if(ii==string::npos) ii=P2.size();
					if( ii-i > c2 ) c2=ii-i, i2=i;
				}
			if( zc <= a1 )
				return 0;
			if( zc <= c1 )
				return i1;
			if( zc <= b1+a1 )
				return P1.size() - b1;
			if( zc <= b1+a2 )
				return P1.size()*1000000 - b1;
			if( zc <= c2 )
				return P1.size()*1000000 + i2;
			if( zc <= b2+a1 )
				return P1.size()*1000000 + P2.size() - b2;
			if( zc <= b2+a2 )
				return P1.size()*1000000 + P2.size() + P1.size()*1000000 + P2.size() - b2;
			return -1;
		}
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string P1 = "111010100001010"; 
	string P2 = "010000001000"; 
	string zeroCount = "3"; 
	long long RetVal = 7LL; 
	return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); }
int Test_(Case_<1>) {
	string P1 = "1101010010"; 
	string P2 = "0001000"; 
	string zeroCount = "3"; 
	long long RetVal = 9999999LL; 
	return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); }
int Test_(Case_<2>) {
	string P1 = "1101010010"; 
	string P2 = "0001000"; 
	string zeroCount = "5"; 
	long long RetVal = 20000011LL; 
	return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); }
int Test_(Case_<3>) {
	string P1 = "10101010"; 
	string P2 = "101010101010"; 
	string zeroCount = "9876543219876"; 
	long long RetVal = -1LL; 
	return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); }
int Test_(Case_<4>) {
	string P1 = "11111111111111111111111111"; 
	string P2 = "0"; 
	string zeroCount = "9876543219876"; 
	long long RetVal = -1LL; 
	return verify_case(RetVal, RepeatedPatterns().findZeroSegment(P1, P2, zeroCount)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE