4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const LL MODVAL = 835454957; 4fd800b3a8 2011-02-23 kinaba: int memo[50*15*101*2]; 4fd800b3a8 2011-02-23 kinaba: int memo_k[101]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class CountPalindromes 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: vector<string> w; 4fd800b3a8 2011-02-23 kinaba: int count(vector<string> words, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: w = words; 4fd800b3a8 2011-02-23 kinaba: memset(memo, 0xff, sizeof(memo)); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: memo_k[0] = 0; 4fd800b3a8 2011-02-23 kinaba: for(int kk=1; kk<=k; ++kk) { 4fd800b3a8 2011-02-23 kinaba: LL ak = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<w.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: ak += rec_l(i, 0, kk); 4fd800b3a8 2011-02-23 kinaba: ans += (memo_k[kk] = ak%MODVAL); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return int(ans % MODVAL); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int rec_l(int i, int ix, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int key = (((i*15)+ix)*101+k)*2+0; 4fd800b3a8 2011-02-23 kinaba: if( memo[key]>=0 ) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<w.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: ans += rec(i, ix, j, w[j].size()-1, k); 4fd800b3a8 2011-02-23 kinaba: return memo[key] = int(ans % MODVAL); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int rec_r(int j, int jx, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int key = (((j*15)+jx)*101+k)*2+1; 4fd800b3a8 2011-02-23 kinaba: if( memo[key]>=0 ) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL ans = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<w.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: ans += rec(i, 0, j, jx, k); 4fd800b3a8 2011-02-23 kinaba: return memo[key] = int(ans % MODVAL); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int rec(int i, int ix, int j, int jx, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: while( k>=2 && ix<w[i].size() && 0<=jx ) 4fd800b3a8 2011-02-23 kinaba: if( w[i][ix] != w[j][jx] ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: ++ix, --jx, k-=2; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( k == 0 ) 4fd800b3a8 2011-02-23 kinaba: return (i==j && ix==jx+1 ? 1 : 0); 4fd800b3a8 2011-02-23 kinaba: if( k == 1 ) 4fd800b3a8 2011-02-23 kinaba: return (i==j && ix==jx || ix==w[i].size() && jx==-1 ? 1 : 0); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if( ix==w[i].size() && jx==-1 ) 4fd800b3a8 2011-02-23 kinaba: return memo_k[k-2]; 4fd800b3a8 2011-02-23 kinaba: else if( ix==w[i].size() ) 4fd800b3a8 2011-02-23 kinaba: return rec_r(j, jx, k-1); 4fd800b3a8 2011-02-23 kinaba: else if( jx==-1 ) 4fd800b3a8 2011-02-23 kinaba: return rec_l(i, ix, k-1); 4fd800b3a8 2011-02-23 kinaba: else 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: private: 4fd800b3a8 2011-02-23 kinaba: 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(); } 4fd800b3a8 2011-02-23 kinaba: 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; } } 4fd800b3a8 2011-02-23 kinaba: 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)); } 4fd800b3a8 2011-02-23 kinaba: 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)); } 4fd800b3a8 2011-02-23 kinaba: 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)); } 4fd800b3a8 2011-02-23 kinaba: 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)); } 4fd800b3a8 2011-02-23 kinaba: 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)); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: int main() { CountPalindromes().run_test(-1); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE