好菜这么简单的题都想不出来
dp式代表i,j内回文串字数最多的次数
dp[i][j] = dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1]及是有左端右端减去中间重合部分
如果i,j相等说明可以与[i+1,j-1]中的字符串组合成新的子串,并且str[i] 与str[j]也能组合成一个字符串
#include<cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define fst first
#define sec second
#define sci(num) scanf("%d",&num)
#define scl(num) scanf("%lld",&num)
#define scf(num) scanf("%lf",&num)
#define mem(a,b) memset(a,b,sizeof a)
#define cpy(a,b) memcopy(a,b,sizeof b)
typedef long long LL;
typedef pair<int,int> P;
const int MAX_E = 1e6 + 100;
const int MAX_N = 1e3 + 100;
const LL MOD = 10007;
const int INF = 0x3f3f3f3f;
char str[MAX_N];
int dp[MAX_N][MAX_N];
int main(){
int T;
sci(T);
for (int cs = 1; cs <= T;cs++) {
scanf("%s",str + 1);
int N = strlen(str + 1);
mem(dp,0);
for (int i = 1;i <= N;i++)
dp[i][i] = 1;
for (int len = 1;len <= N;len++) {
for (int i = 1;i + len <= N;i++) {
int j = i + len;
dp[i][j] = (dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] + MOD) % MOD;
if (str[i] == str[j]) {
dp[i][j] += dp[i + 1][j - 1] + 1;
dp[i][j] %= MOD;
}
}
}
printf("Case %d: %d\n",cs,dp[1][N]);
}
return 0;
}