比较好的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 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(); 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 HERE
int main()
{
FoxAndMountain ___test;
___test.run_test(-1);
return 0;
}
// END CUT HERE