Artifact Content
Not logged in

Artifact f68efed934ab056b0a0f09dbfed2cfe08d7285bd


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

class CuttingBitString { public:
	static const int INF = 999;

	int getmin(string S)
	{
		LL v = 1;
		set<string> good;
		for(LL v=1; v<(1LL<<60); v*=5) {
			cerr << bin(v) << endl;
			good.insert(bin(v));
		}

		int s = best(S, good, 0, S.size());
		return s>=INF ? -1 : s;
	}

	map<pair<int,int>, int> memo;
	int best(const string& S, const set<string>& good, int s, int e)
	{
		pair<int,int> key(s,e);
		if(memo.count(key))
			return memo[key];

		if( good.count(S.substr(s, e-s)) )
			return memo[key] = 1;
		int score = INF;
		for(int m=s+1; m+1<=e; ++m)
		{
			int x = best(S, good, s, m);
			int y = best(S, good, m, e);
			score = min(score, x+y);
		}
		return memo[key] = score;
	}

	string bin(LL v)
	{
		string s;
		for(; v; v>>=1)
			s += (v&1 ? '1' : '0');
		reverse(s.begin(), s.end());
		return s;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, CuttingBitString().getmin(S));}
int main(){

CASE(3)
	string S = "110011011"; 
	int _ = 3; 
END
CASE(0)
	string S = "101101101"; 
	int _ = 3; 
END
CASE(1)
	string S = "1111101"; 
	int _ = 1; 
END
CASE(2)
	string S = "00000"; 
	int _ = -1; 
END
CASE(4)
	string S = "1000101011"; 
	int _ = -1; 
END
CASE(5)
	string S = "111011100110101100101110111"; 
	int _ = 5; 
END
CASE(6)
	string S = "11111111111111111111111111111111111111111111111111"; 
	int _ = -999; 
END
CASE(7)
	string S = "1101100011010111001001101011011100010111011110101"; 
	int _ = 1; 
END
}
// END CUT HERE