Time limit5000 ms Memory limit1048576 kB OSLinux
Yraglac is expecting to have a child in the near future. Being a mathematically-minded person, he would like his child’s name to be a palindrome – that is, reads the same when read forward and backward. Given a name, he would like to count the number of ways he can create a palindromic name by removing zero or more letters. For example, given the name “abcdb”, he can remove the letters “a” and “c” to get the palindrome “bdb”. Of course, the resulting name must not be empty. The answer might be very large so you should output the answer modulo 1000000007=109+71000000007=109+7.
Input
The first line contains a single integer T≤10T≤10 giving the number of test cases. Each test case consists of a single name with length between 11 and 20002000 inclusive, containing only lowercase letters.
Output
For each test case, output a single line containing the answer modulo 10000000071000000007.
Sample Input 1
5
bob
a
aa
aaa
abcdb
Sample Output 1
5
1
3
7
8
题意:给一个字符串,问删掉0个及以上的字符让字符串变成回文串(字符串不能为空)有多少种方法?
分析:回文串上次写了一个,这次又是一个,还是没写出来, 但是比起上次那道题,难一些,这就是我不会DP的借口?
DP,设dp[i][j]为删除[i,j]个字符的方法数量
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int N=2005;
typedef long long LL;
LL dp[N][N];
int main()
{
int t;
char str[N];
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
int len=strlen(str);
memset(dp,0,sizeof(dp));
for(int i=0; i<len; i++)
dp[i][i]=1;
for(int i=len-1; i>=0; i--)
for(int j=i+1; j<len; j++)
{
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][j]+dp[i+1][j-1]+1)%mod;
}
printf("%lld\n",dp[0][len-1]);
}
return 0;
}