UVA10617 Again Palindrome DP

简单的DP 统计回文序列数量(注意是序列不是串)

f[i][j]= f[i+1][j]+f[i][j-1]+1   s[i]==s[j]

f[i][j]= f[i+1][j]+f[i][j-1]-f[i+1][j-1]     s[i]!=s[j]


Problem I

Again Palindromes

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

A palindorme is a sequence of one or more characters that reads the same from the left as it does from the right. For example, ZTOT andMADAM are palindromes, but ADAM is not.

 

Given a sequence S of N capital latin letters. How many ways can one score out a few symbols (maybe 0) that the rest of sequence become a palidrome. Varints that are only  different by an order of scoring out should be considered the same.

 

Input

The input file contains several test cases (less than 15). The first line contains an integer T that indicates how many test cases are to follow.

 

Each of the T lines contains a sequence S (1≤N≤60). So actually each of these lines is a test case.

 

Output

For each test case output in a single line an integer – the number of ways.

 

Sample Input                             Output for Sample Input

3

BAOBAB

AAAA

ABA

22

15

5


Russian Olympic Camp


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>

using namespace std;

typedef long long ll;

char str[100];
ll f[100][100];

int main()
{
    int cs;
    scanf("%d",&cs);
    while(cs--)
    {
        scanf("%s",str);
        memset(f,0,sizeof(f));
        int len=strlen(str);
        for(int i=0;i<len;i++)
            f[i][i]=1;
        for(int i=2;i<=len;i++)
        {
            for(int j=0;j+i-1<len;j++)
            {
                if(str[j]==str[j+i-1])
                    f[j][j+i-1]=f[j+1][j+i-1]+f[j][j+i-2]+1;
                else
                    f[j][j+i-1]=f[j+1][j+i-1]+f[j][j+i-2]-f[j+1][j+i-2];
            }
        }
        printf("%lld\n",f[0][len-1]);
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值