250pt: Water question
500pt: Status compression enumeration. the system test fails. success...
1000pt: You can abstract the question in this way, construct a string with a length of len and a total weight of 0 containing the mode string, and obtain the number of such strings, A string consists of 'U' and 'D'. 'U': + 1 'D':-1.
DP, which is a set of AC automatic machines --
The status is very simple. When transferring, pay attention to whether the status to be transferred contains a mode string.
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <string>
# Include <vector>
Using namespace std;
Const int mod = 1000000009;
Const int M = 100;
Const int CD = 26;
Int sz;
Int Q [M];
Int ch [M] [CD];
Int ID [128];
Int fail [M];
Int val [M];
Void Init (){
Fail [0] = 0; val [0] = 0;
Sz = 1;
Memset (ch [0], 0, sizeof (ch [0]);
For (int I = 0; I <26; I ++) ID [I + 'a'] = I;
}
Void Insert (const char * s ){
Int p = 0;
For (; * s; s ++ ){
Int c = ID [* s];
If (! Ch [p] [c]) {
Memset (ch [sz], 0, sizeof (ch [sz]);
Fail [sz] = val [sz] = 0;
Ch [p] [c] = sz ++;
}
P = ch [p] [c];
}
Val [p] = 1;
}
Void Construct (){
Int * s = Q, * e = Q, v;
For (int I = 0; I <CD; I ++ ){
If (ch [0] [I]) {
Fail [ch [0] [I] = 0;
* E ++ = ch [0] [I];
}
}
While (s! = E ){
Int u = * s ++;
For (int I = 0; I <CD; I ++ ){
If (v = ch [u] [I]) {
* E ++ = v;
Fail [v] = ch [fail [u] [I];
Val [v] | = val [fail [v];
} Else {
Ch [u] [I] = ch [fail [u] [I];
}
}
}
}
Class FoxAndMountain {
Public:
Int dp [55] [55] [55] [2]; // length node and whether to include mode string
Int count (int n, string S ){
Init ();
Insert (S. c_str ());
Construct ();
// For (int I = 0; I <sz; I ++) printf ("fail [% d] = % d", I, fail [I]); puts ("");
Int H = 26;
Dp [0] [0] [0] [0] = 1;
For (int I = 0; I <= n; I ++)
{
For (int j = 0; j <sz; j ++)
{
For (int h = 0; h <H; h ++)
{
For (int flag = 0; flag <2; flag ++)
{
If (! Dp [I] [j] [h] [flag]) continue;
Int x = ch [j] [ID ['U'], fx = val [x] | flag;
Int y = ch [j] [ID ['D'], fy = val [y] | flag;
Dp [I + 1] [x] [h + 1] [fx] = (dp [I + 1] [x] [h + 1] [fx] + dp [I] [j] [h] [flag]) % mod;
If (h) dp [I + 1] [y] [h-1] [fy] = (dp [I + 1] [y] [h-1] [fy] + dp [I] [j] [h] [flag]) % mod;
}
}
}
}
Int ans = 0;
For (int I = 0; I <sz; I ++)
{
Ans + = dp [n] [I] [0] [1]; ans % = mod;
}
Return ans;
}
};