Artifact Content
Not logged in

Artifact a8db06433cf2bbb2a1eb4a0084de28b9c1b868f4


#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>
using namespace std;
typedef long long LL;
#include <bitset>

static const int INF = 3000;
class ReversalChain
{
public:
	map<pair<pair<LL,LL>,int>,int> memo;
	int minReversal(string init, string goal) 
	{
		LL s=0, sb=0; for(int i=0; i<init.size(); ++i) if(init[i]=='1') s |= (1LL<<i), sb++;
		LL g=0, gb=0; for(int i=0; i<goal.size(); ++i) if(goal[i]=='1') g |= (1LL<<i), gb++;
		if( sb != gb )
			return -1;
		int a = mrev(s, g, init.size());
		return a>=INF ? -1 : a;
	}
	int mrev(LL s, LL g, int m)
	{
		#define mask(x)  ((x) & (1LL<<(m))-1)
		#define ith(x,i) (((x)>>(i))&1)

		if( mask(s) == mask(g) )
			return 0;

		pair<pair<LL,LL>,int> key(make_pair(mask(s),mask(g)),m);
		if( memo.count(key) )
			return memo[key];

		int a = INF;
		if( ith(s,m-1)==ith(g,m-1) ) a = min(a, mrev(s, g, m-1));
		if( ith(s,0)==ith(g,0) )     a = min(a, mrev(s>>1, g>>1, m-1));

		s = bitrev(s,m);
		if( ith(s,m-1)==ith(g,m-1) ) a = min(a, 1+mrev(s, g, m-1));
		if( ith(s,0)==ith(g,0) )     a = min(a, 1+mrev(s>>1, g>>1, m-1));

		return memo[key] = a;
	}
	LL bitrev(LL x, LL m)
	{
		LL y = 0;
		for(int i=0; i<m; ++i)
			y |= (((x>>i)&1) << (m-1-i));
		return y;
	}
};

// 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 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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string init = "1100"; 
	string goal = "0110"; 
	int RetVal = 1; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }
int Test_(Case_<1>) {
	string init = "111000"; 
	string goal = "101010"; 
	int RetVal = 2; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }
int Test_(Case_<2>) {
	string init = "0"; 
	string goal = "1"; 
	int RetVal = -1; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }
int Test_(Case_<3>) {
	string init = "10101"; 
	string goal = "10101"; 
	int RetVal = 0; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }
int Test_(Case_<4>) {
	string init = "111000111000"; 
	string goal = "001100110011"; 
	int RetVal = 4; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }
int Test_(Case_<5>) {
	string init = "001000011000000111000000001111";
	string goal = "100100100100100100100100100100"; 
	int RetVal = 9; 
	return verify_case(RetVal, ReversalChain().minReversal(init, goal)); }

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