码迷,mamicode.com
首页 > 其他好文 > 详细

Uva 10617 Again Palindrome(区间dp)

时间:2015-01-26 22:50:54      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:uva 10617 again pali   区间dp   

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, Z, TOT and MADAM 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


题意:

求回文字串的个数。


CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 10000007
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 1000010;

using namespace std;

char s[110];
ll dp[110][110];

int main()
{
    int t;
    while(cin>>t)
    {
        while(t--)
        {
            scanf("%s",s+1);
            int n=strlen(s+1);
            dp[0][0]=0;
            for(int i=1;i<=n;i++)
                dp[i][i]=1;
            for(int l=1; l<=n; l++)
            {
                for(int i=1,j=i+l; j<=n; j++,i++)
                {
                    if(s[i]==s[j])
                        dp[i][j]=dp[i+1][j-1]*2+3;
                    else
                        dp[i][j]=dp[i+1][j-1]+2;
                    for(int k=i+1; k<j; k++)
                    {
                        if(s[i]==s[k])
                            dp[i][j]+=dp[i+1][k-1]+1;
                        if(s[j]==s[k])
                            dp[i][j]+=dp[k+1][j-1]+1;
                    }
                }
            }
            cout<<dp[1][n]<<endl;
        }
    }
    return 0;
}


Uva 10617 Again Palindrome(区间dp)

标签:uva 10617 again pali   区间dp   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/43159999

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!