Artifact Content
Not logged in

Artifact c85a397b1091370e779c13cec319a120feaf33f3


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

static const LL MODVAL = 835454957;
int memo[50*15*101*2];
int memo_k[101];

class CountPalindromes
{
public:
	vector<string> w;
	int count(vector<string> words, int k) 
	{
		w = words;
		memset(memo, 0xff, sizeof(memo));

		LL ans = 0;
		memo_k[0] = 0;
		for(int kk=1; kk<=k; ++kk) {
			LL ak = 0;
			for(int i=0; i<w.size(); ++i)
				ak += rec_l(i, 0, kk);
			ans += (memo_k[kk] = ak%MODVAL);
		}
		return int(ans % MODVAL);

	}

	int rec_l(int i, int ix, int k)
	{
		int key = (((i*15)+ix)*101+k)*2+0;
		if( memo[key]>=0 )
			return memo[key];

		LL ans = 0;
		for(int j=0; j<w.size(); ++j)
			ans += rec(i, ix, j, w[j].size()-1, k);
		return memo[key] = int(ans % MODVAL);
	}

	int rec_r(int j, int jx, int k)
	{
		int key = (((j*15)+jx)*101+k)*2+1;
		if( memo[key]>=0 )
			return memo[key];

		LL ans = 0;
		for(int i=0; i<w.size(); ++i)
			ans += rec(i, 0, j, jx, k);
		return memo[key] = int(ans % MODVAL);
	}


	int rec(int i, int ix, int j, int jx, int k)
	{
		while( k>=2 && ix<w[i].size() && 0<=jx )
			if( w[i][ix] != w[j][jx] )
				return 0;
			else
				++ix, --jx, k-=2;

		if( k == 0 )
			return (i==j && ix==jx+1 ? 1 : 0);
		if( k == 1 )
			return (i==j && ix==jx || ix==w[i].size() && jx==-1 ? 1 : 0);

		if( ix==w[i].size() && jx==-1 )
			return memo_k[k-2];
		else if( ix==w[i].size() )
			return rec_r(j, jx, k-1);
		else if( jx==-1 )
			return rec_l(i, ix, k-1);
		else
			return 0;
	}

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
	private:
	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(); }
	void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { string Arr0[] = {"tragic","cigar"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 24; int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); }
	void test_case_1() { string Arr0[] = {"z","zz"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; int Arg2 = 5; verify_case(1, Arg2, count(Arg0, Arg1)); }
	void test_case_2() { string Arr0[] = {"aba","acaba","baca","cac","b","c","a"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 70; int Arg2 = 370786966; verify_case(2, Arg2, count(Arg0, Arg1)); }
	void test_case_3() { string Arr0[] = {"hello"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 0; verify_case(3, Arg2, count(Arg0, Arg1)); }
	void test_case_4() { string Arr0[] = {"a", "aaaaaaaaab", "aaa", "aaaaaaaaaaaa", "aabbaaa", "babaaaaaaaa", "aaaaaabaaaba", "aaaa", "aaaaaaaaa", "aaaaaaaaaa", "aab", "aaaaaababaaaaa", "aaaaaaaaaaabba", "aaaaaabaa", "aaabaaaaaa", "abaaaaaaaa", "aaaaaa", "aabaaaaaaaaaba", "ab", "bbbaabaaaab", "aaaabaaaaa", "aa", "baaaaaaaaaaaa", "aabaa", "aabaaaaaaaabbaa", "aaaaaaaabaaaaaa", "aaaaaaaa", "aaaaa", "aaaaaaaaaaa", "aabaaaaa", "baabaaaaaaaaa", "baaaaa", "aaaaaaaabaaaaa", "aaaaaaaaaaaaaaa", "abaaaaaaaaaaa", "aaabababaaaa", "aaaaaaaaaabaaaa", "abaaa", "aaaaabaaaab", "aaaabaaaaaaa", "aaaaaaaba", "babbbaaaaaba", "aaaaaaaaaaaaa", "aaababaabaab", "aaabaabaaab", "aaaabbaaabaaaaa", "abaaaaaaaaaaaaa", "aaaaaaa", "abaaaaabaaaaaaa", "aaabaaaaaaaaa"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 100; int Arg2 = 570277357; verify_case(4, Arg2, count(Arg0, Arg1)); }
// END CUT HERE
};
// BEGIN CUT HERE 
int main() { CountPalindromes().run_test(-1); }
// END CUT HERE