比較好的dp
當k=m時,dp[i][j][m]記錄走i位字串上目前和為j且目標串是目前字串的字串的個數為k
當k!=m時,dp[i][j][k]記錄走i位字串上目前和為j且尾碼和目標串匹配的個數為k的個數
而e[i][0]表示目標串的前i位+‘U’ 尾碼匹配目標串的最大長度
e[i][1]同理類似。
// BEGIN CUT HERE// END CUT HERE#line 5 "FoxAndMountain.cpp"#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;typedef long long ll;int e[200][2];ll dp[110][110][110];int match(string a,string b){ while(a.substr(0,b.length())!=b) b=b.substr(1); return b.length();}class FoxAndMountain{ public: int count(int n, string history) { int i,j,k; int m=history.size(); for(i=0;i<m;i++){ e[i][0]=match(history,history.substr(0,i)+'U'); e[i][1]=match(history,history.substr(0,i)+'D'); } e[m][0]=e[m][1]=m; memset(dp,0,sizeof(dp)); dp[0][0][0]=1; for(i=0;i<n;i++) for(j=0;j<=i;j++){ for(k=0;k<=m && k<=i;k++){ dp[i+1][j+1][e[k][0]]+=dp[i][j][k]; if(j)dp[i+1][j-1][e[k][1]]+=dp[i][j][k]; } } return (int)(dp[n][0][m]%1000000009); }// BEGIN CUT HEREpublic: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(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }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() { int Arg0 = 4; string Arg1 = "UUDD"; int Arg2 = 1; verify_case(0, Arg2, count(Arg0, Arg1)); }void test_case_1() { int Arg0 = 4; string Arg1 = "DUUD"; int Arg2 = 0; verify_case(1, Arg2, count(Arg0, Arg1)); }void test_case_2() { int Arg0 = 4; string Arg1 = "UU"; int Arg2 = 1; verify_case(2, Arg2, count(Arg0, Arg1)); }void test_case_3() { int Arg0 = 49; string Arg1 = "U"; int Arg2 = 0; verify_case(3, Arg2, count(Arg0, Arg1)); }void test_case_4() { int Arg0 = 20; string Arg1 = "UUUDUUU"; int Arg2 = 990; verify_case(4, Arg2, count(Arg0, Arg1)); }void test_case_5() { int Arg0 = 30; string Arg1 = "DUDUDUDUDUDUDUDU"; int Arg2 = 3718; verify_case(5, Arg2, count(Arg0, Arg1)); }void test_case_6() { int Arg0 = 50; string Arg1 = "U"; int Arg2 = 946357703; verify_case(6, Arg2, count(Arg0, Arg1)); }// END CUT HERE};// BEGIN CUT HEREint main(){ FoxAndMountain ___test; ___test.run_test(-1); return 0;}// END CUT HERE