Artifact Content
Not logged in

Artifact 286098999b75ba9e70158e78ece5c3d0c2004c15


#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class PalindromePhrases {
public:
	vector<string> W;
	int N;
	map<pair<int,string>, LL> memo1;
	map<pair<int,string>, LL> memo2;
	map<pair<int,string>, LL> memo3;
	map<pair<int,string>, LL> memo4;
	map<pair<int,string>, LL> memo5;

	string rev(string s)
	{
		reverse(s.begin(), s.end());
		return s;
	}

	bool prefix_all(const string& p, const string& a)
	{
		return p.size() <= a.size() && equal(p.begin(), p.end(), a.begin());
	}

	bool suffix_all(const string& p, const string& a)
	{
		return p.size() <= a.size() && equal(p.begin(), p.end(), a.begin()+a.size()-p.size());
	}

	long long getAmount(vector <string> words) 
	{
		W = words;
		N = W.size();

		LL cnt = 0;
		for(int i=0; i<N; ++i)
		{
			int mask = (1<<N) - 1;
			mask &= ~ (1<<i);

			cnt += rec_startpalin(mask, W[i]);
		}
		return cnt;
	}

	LL rec_startpalin(int mask, const string& w) // #{ws | ws is palin}
	{
		pair<int,string> key(mask, w);
		if( memo1.count(key) )
			return memo1[key];

		string rw = rev(w);

		LL cnt = 0;
		cnt += rec_end(mask, rw);
		for(int ov=1; ov<=w.size(); ++ov)
			if( w.substr(w.size()-ov) == rw.substr(0,ov) )
				cnt += exact(mask, rw.substr(ov));
		return memo1[key]=cnt;
	}

	LL rec_endpalin(int mask, const string& w) // #{sw | sw is palin}
	{
		pair<int,string> key(mask, w);
		if( memo4.count(key) )
			return memo4[key];

		string rw = rev(w);

		LL cnt = 0;
		cnt += rec(mask, rw);
		for(int ov=1; ov<=w.size(); ++ov)
			if( rw.substr(w.size()-ov) == w.substr(0,ov) )
				cnt += exact(mask, rw.substr(0,w.size()-ov));
		return memo4[key]=cnt;
	}

	LL rec(int mask, const string& w) // #{ws | s is palin}
	{
		pair<int,string> key(mask, w);
		if( memo2.count(key) )
			return memo2[key];

		LL cnt = (w.empty() ? 1 : 0);
		for(int i=0; i<N; ++i)
			if( mask & (1<<i) )
			{
				if( prefix_all(W[i], w) )
					cnt += rec( mask &~ (1<<i), 
						w.substr(W[i].size())
					);
				else if( prefix_all(w, W[i]) )
					cnt += rec_startpalin( mask &~ (1<<i),
						W[i].substr(w.size())
					);
			}
		return memo2[key]=cnt;
	}

	LL rec_end(int mask, const string& w) // #{sw | s is palin}
	{
		pair<int,string> key(mask, w);
		if( memo5.count(key) )
			return memo5[key];

		LL cnt = (w.empty() ? 1 : 0);
		for(int i=0; i<N; ++i)
			if( mask & (1<<i) )
			{
				if( suffix_all(W[i], w) )
					cnt += rec_end( mask &~ (1<<i),
						w.substr(0,w.size()-W[i].size())
					);
				else if( suffix_all(w, W[i]) )
					cnt += rec_endpalin( mask &~ (1<<i),
						W[i].substr(0, W[i].size()-w.size())
					);
			}
		return memo5[key]=cnt;
	}

	LL exact(int mask, const string& w) // # of ways to create w
	{
		pair<int,string> key(mask, w);
		if( memo3.count(key) )
			return memo3[key];

		LL cnt = (w.empty() ? 1 : 0);
		for(int i=0; i<N; ++i)
			if( mask & (1<<i) )
				if( prefix_all(W[i], w) )
					cnt += exact( mask &~ (1<<i), w.substr(W[i].size()) );

		return memo3[key] = cnt;
	}


};

// 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 words_[] = {"a","ba"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long RetVal = 2LL; 
	return verify_case(RetVal, PalindromePhrases().getAmount(words)); }
int Test_(Case_<1>) {
	string words_[] = {"ab","bcd","efg"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long RetVal = 0LL; 
	return verify_case(RetVal, PalindromePhrases().getAmount(words)); }
int Test_(Case_<2>) {
	string words_[] = {"a", "bba", "abb"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long RetVal = 7LL; 
	return verify_case(RetVal, PalindromePhrases().getAmount(words)); }
int Test_(Case_<3>) {
	string words_[] = {"aabccc", "ccbbca", "a", "acaabb", "aaa", "aab", "c", "babb", "aacaa", "b"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long RetVal = 47LL; 
	return verify_case(RetVal, PalindromePhrases().getAmount(words)); }
int Test_(Case_<4>) {
	string words_[] = {"a", "aa", "aaa"};
	  vector <string> words(words_, words_+sizeof(words_)/sizeof(*words_)); 
	long long RetVal = 47LL; 
	return verify_case(RetVal, PalindromePhrases().getAmount(words)); }

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