Solution S o l u t i o n
考虑其反面,就是不存在一个子序列是回文串。
实际上需要考虑的只有连续的两个和三个字符。因为其他情况都已经包含在这个之中。
设
dpi,l,0/1,0/1,0/1
d
p
i
,
l
,
0
/
1
,
0
/
1
,
0
/
1
表示考虑到
i
i
,长度为,
i,i−1,i−2
i
,
i
−
1
,
i
−
2
是否选取的状态为
0/1
0
/
1
。
直接DP,最后把长度大于2的上升序列方案数减掉DP出来的不合法方案数就好啦。
写的时候偷了懒,其实
l
l
那一维只需要存三种情况就好啦。那就可以
O(n)
O
(
n
)
啦。
// BEGIN CUT HERE
// END CUT HERE
#line 5 "ConsecutivePalindromes.cpp"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2020;
const int MOD = 1000000007;
int f[N][N][2][2][2];
char s[N];
int n, res, ans, fac;
inline int add(int &x, int a) {
x = (x + a) % MOD;
}
class ConsecutivePalindromes {
public:
int get(int pos) {
if (s[pos - 1] == s[pos]) return 2;
if (pos >= 3 && s[pos - 2] == s[pos]) return 3;
return 0;
}
int countStrings(string S) {
n = S.size();
for (int i = 0; i < n; i++)
s[i + 1] = S[i];
memset(f, 0, sizeof f);
f[1][0][0][0][0] = f[1][1][0][0][1] = 1;
for (int i = 1; i < n; i++) {
int lim = get(i + 1);
for (int l = 0; l <= i; l++)
for (int x = 0; x < 2; x++)
for (int y = 0; y < 2; y++)
for (int z = 0; z < 2; z++)
for (int t = 0; t < 2; t++)
if (lim == 2) {
if ((z & t) == 1) continue;
add(f[i + 1][l + t][y][z][t], f[i][l][x][y][z]);
} else if (lim == 3) {
if ((y & z & t) == 1) continue;
add(f[i + 1][l + t][y][z][t], f[i][l][x][y][z]);
} else {
add(f[i + 1][l + t][y][z][t], f[i][l][x][y][z]);
}
}
res = 0;
for (int l = 2; l <= n; l++)
for (int i = 0; i < 2; i++)
for (int j = 0; j < 2; j++)
for (int k = 0; k < 2; k++)
add(res, f[n][l][i][j][k]);
int pw = 1;
for (int i = 1; i <= n; i++) pw = 2 * pw % MOD;
ans = (pw - n - 1 + MOD) % MOD;
ans = (ans - res + MOD) % MOD;
return ans;
}
// 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() { string Arg0 = "AAA"; int Arg1 = 3; verify_case(0, Arg1, countStrings(Arg0)); }
void test_case_1() { string Arg0 = "ABCDEF"; int Arg1 = 0; verify_case(1, Arg1, countStrings(Arg0)); }
void test_case_2() { string Arg0 = "BBAA"; int Arg1 = 7; verify_case(2, Arg1, countStrings(Arg0)); }
void test_case_3() { string Arg0 = "ABCBA"; int Arg1 = 4; verify_case(3, Arg1, countStrings(Arg0)); }
void test_case_4() { string Arg0 = "TOPPAPPOT"; int Arg1 = 240; verify_case(4, Arg1, countStrings(Arg0)); }
void test_case_5() { string Arg0 = "AYUEOPPOLAKIKIKIUYOPZOOLPPPPKMOPOLIURKMQOAPLFKTURIWOOWWWPLOLWWWOPSLAA"; int Arg1 = 216072426; verify_case(5, Arg1, countStrings(Arg0)); }
void test_case_6() { string Arg0 = "Z"; int Arg1 = 0; verify_case(6, Arg1, countStrings(Arg0)); }
// END CUT HERE
};
// BEGIN CUT HERE
int main(void) {
ConsecutivePalindromes ___test;
___test.run_test(-1);
system("pause");
}
// END CUT HERE