Uva 10617 Again Palindrome(区间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;
}
时间: 2024-10-30 05:53:13

Uva 10617 Again Palindrome(区间dp)的相关文章

uva 10617 Again Palindrome (DP)

uva 10617 Again Palindrome 题目大意:给出一段字符串,可进行删除操作,可以删除任意位置任意个数(可以是0)的字符.问,进行删除操作使原本字符串变成回文字符串,有几种方式. 解题思路: dp[i][j] = 1 (i == j),单独一个字符也是回文字符串 s[i] != s[j]时, dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1], dp[i + 1][j] 和 dp[i][j + 1]的公共部分dp[

UVa 10617 Again Palindrome(回文串区间DP)

UVa 10617 Again Palindrome(经典回文串区间DP) 题意: 给定一个字符串s,对s进行删除操作,使得剩下的子串是回文字符串,问最多有多少种这种子串. 思路: 涉及到回文字符串,首先要想到的肯定是区间DP,如何写出状态转移方程? 直接从题意切入:dp[i, j]表示区间[i, j]最多有多少个这样的子串. 1. s[i] == s[j] 去掉s[i],则一个子问题就是dp[i+1, j]; 去掉s[j],另一个子问题就是dp[i, j-1]; 显然这两个子问题是会有重叠的,

UVA 10453 Make Palindrome(区间简单DP)

题意:给出一个字符串A,求出需要至少插入多少个字符使得这个字符串变成回文串. 思路:设dp[i][j]为使区间[i, j]变成回文串所需要的最少字符个数. 1.A[i] == A[j的情况]那么dp[i][j] = min(dp[i][j], dp[i + 1][j -1]); 2.或者在第j个位置插入一个字符A[i], dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1); 3.或者在第i个位置插入一个字符A[j], dp[i][j] = min(dp[i][j

UVA 10003 Cutting Sticks(区间dp)

Description  Cutting Sticks  You have to cut a wood stick into pieces. The most affordable company, The Analog Cutting Machinery, Inc. (ACM), charges money according to the length of the stick being cut. Their procedure of work requires that they onl

UVA 10003 Cutting Sticks 区间DP+记忆化搜索

UVA 10003 Cutting Sticks+区间DP 纵有疾风起 题目大意 有一个长为L的木棍,木棍中间有n个切点.每次切割的费用为当前木棍的长度.求切割木棍的最小费用 输入输出 第一行是木棍的长度L,第二行是切割点的个数n,接下来的n行是切割点在木棍上的坐标. 输出切割木棍的最小费用 前话-区间dp简单入门 区间dp的入门下面博客写的非常好,我就是看的他们博客学会的,入门简单,以后的应用就得靠自己了. https://blog.csdn.net/qq_41661809/article/d

POJ 1159 Palindrome(区间DP/最长公共子序列+滚动数组)

Palindrome Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 56150   Accepted: 19398 Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a

Uva 10003-Cutting Sticks(区间DP)

题目链接:点击打开链接 题意: 一根长度为L 的木棒,要求要切割n次,给出n次需要切割的位置,每次切割花费为当前段的总长度.求最小花费. 思路:..一开始想了好久也没往区间DP那方面想QAQ.设 dp[i][j] 为切割[i,j] 区间的最小花费,状态转移方程为 dp[i][j]=max(dp[i][k-1]+dp[k+1][j]+a[j+1]-a[i-1]); #include <algorithm> #include <iostream> #include <cstrin

UVA 1331 Minimax Triangulation 区间DP

区间DP: 将一个多边形三角剖分,让可以得到的最大三角形的面积最小 dp[i][j]表示从i点到j点的最优值,枚举中间点k dp[i][j]=min(dp[i][j],max(area(i,j,k),max(dp[i][k],dp[k][j]))); 注意如果中间三角形i-j-k中有其他的点,这样的三角形是不可以剖分的 Minimax Triangulation Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld

POJ 3280 Cheapest Palindrome ( 区间DP &amp;&amp; 经典模型 )

题意 : 给出一个由 n 中字母组成的长度为 m 的串,给出 n 种字母添加和删除花费的代价,求让给出的串变成回文串的代价. 分析 :  原始模型 ==> 题意和本题差不多,有添和删但是并无代价之分,要求最少操作几次才能是其变成回文串 ① 其实细想之后就会发现如果没有代价之分的话删除和增添其实是一样的,那么除了原串原本拥有的最长回文串不用进行考虑处理,其他都需要进行删除或者增添来使得原串变成回文串,所以只要对原串 S 和其反向串 S' 找出两者的最长公共子串长度 L 再用总长减去 L 即可 ②