(Kattis - palindromic )G - Palindromic Naming(DP)(补)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值