Artifact Content
Not logged in

Artifact 2498e6610d468028b4bbdced29277e5724112372


#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;

template<typename T>
struct DP2
{
	const int N1, N2;
	vector<T> data;
	DP2(int N1, int N2, const T& t = T())
		: N1(N1), N2(N2), data(N1*N2, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2)
		{ return data[ (i1*N2)+i2 ]; }
	void swap(DP2& rhs)
		{ data.swap(rhs.data); }
};

class MapGuessing { public:
	long long countPatterns(string goal, vector <string> code_)
	{
		string code = accumulate(code_.begin(), code_.end(), string(""));
		deque<char> pattern(1, '?');
		int head = 0;
		for(int i=0; i<code.size(); ++i)
			switch(code[i])
			{
			case '0':
			case '1':
				pattern[head] = code[i];
				break;
			case '<':
				head--;
				if(head==-1) {
					head = 0;
					pattern.push_front('?');
				}
				break;
			case '>':
				head++;
				if(head==pattern.size())
					pattern.push_back('?');
				break;
			}
		return solve(goal, string(pattern.begin(), pattern.end()));
	}

	LL solve(const string& G, const string& P)
	{
		if(P.size() > G.size())
			return 0;

		int N = P.size();
		vector< vector<int> > DFA(N, vector<int>(2));
		for(int i=0; i<N; ++i)
		{
			int fail = 0;
			for(int k=i-1; k>=0; --k)
				if( match(P.substr(0, k), P.substr(i-k, k))
					&& match(P[k], nega(P[i])) ) {
					fail = k;
					break;
				}

			if(P[i]=='0')
			{
				DFA[i][0] = i+1;
				DFA[i][1] = fail;
			}
			else if(P[i]=='1')
			{
				DFA[i][0] = fail;
				DFA[i][1] = i+1;
			}
			else
			{
				DFA[i][0] = i+1;
				DFA[i][1] = i+1;
			}
		}

		LL total = 0;
		for(int s=0; s+P.size()<=G.size(); ++s)
		{
			LL v = subProblem(s, G, s+P.size(), DFA, N);
			total += v;
		}
		return total;
	}

	char nega(char c) { return c=='?' ? c : c=='0' ? '1' : '0'; }
	bool match(char a, char b) {
		return a=='?' || b=='?' || a==b;
	}
	bool match(const string& a, const string& b) {
		for(int i=0; i<a.size(); ++i)
			if(!match(a[i], b[i]))
				return false;
		return true;
	}

	LL subProblem(int s, const string& G, int M, const vector< vector<int> >& DFA, int Q)
	{
		DP2<LL> dp(M+1, Q+1);
		dp(0, 0) = 1;
		for(int i=0; i<M; ++i)
		for(int q=0; q<Q; ++q)
		{
			if(s>=i || G[i]=='0')
			dp(i+1, DFA[q][0]) += dp(i,q);
			if(s>=i || G[i]=='1')
			dp(i+1, DFA[q][1]) += dp(i,q);
		}
		return dp(M, Q);
	}
};

// 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 long long& Expected, const long long& 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(_, MapGuessing().countPatterns(goal, code));}
int main(){

CASE(0)
	string goal = "000"; 
	string code_[] = {"0"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 4LL; 
END
CASE(1)
	string goal = "001"; 
	string code_[] = {"0>1"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 5LL; 
END
CASE(2)
	string goal = "000"; 
	string code_[] = {"1>1>1"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 1LL; 
END
CASE(3)
	string goal = "11001"; 
	string code_[] = {">><<<<><<"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 0LL; 
END
CASE(4)
	string goal = "1000101011"; 
	string code_[] = {"1<<0>>0>1"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 22LL; 
END
CASE(5)
	string goal = "00000010000000000000000000000000"; 
	string code_[] = {"><>>0<0<>>1>0><>", "<<0>>0<>><0>0>>><><", ">>>0<>", ">0><>>>>0<<><>>0>>>0<0>>0>"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 13601LL; 
END
CASE(6)
	string goal = "11100011010111111010100100110001101"; 
	string code_[] = {"11111111111111111111"
,"1<><><><><><><><><>1"
,"1<>000>000><0<><0<>1"
,"1<0<><>0><0<00>00<>1"
,"1<>00<>000><0<0<0<>1"
,"1<><>0>0><0<0<><0<>1"
,"1<000<>0><0<0<><0<>1"
,"1<><><><><><><><><>1"
,"1<>000><000<>000><>1"
,"1<>0><><0<><>0><><>1"
,"1<>000><000<>000><>1"
,"1<><>0><><0<><>0><>1"
,"1<>000><000<>000><>1"
,"1<><><><><><><><><>1"
,"11111111111111111111"};
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = 90LL; 
END
/*
CASE(7)
	string goal = ; 
	string code_[] = ;
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = LL; 
END
CASE(8)
	string goal = ; 
	string code_[] = ;
	  vector <string> code(code_, code_+sizeof(code_)/sizeof(*code_)); 
	long long _ = LL; 
END
	*/
}
// END CUT HERE