http://acm.hdu.edu.cn/showproblem.php?pid=4632

题意:

一个字符串,有多少个subsequence是回文串。

别人的题解:

用dp[i][j]表示这一段里有多少个回文串,那首先dp[i][j]=dp[i+1][j]+dp[i][j-1],但是dp[i+1][j]和dp[i][j-1]可能有公共部分,所以要减去dp[i+1][j-1]。

如果str[i]==str[j]的话,还要加上dp[i+1][j-1]+1。

但是自己却是这样想的,把每个区间都要看是否为回文串,在dp[i][j]=dp[i+1][j]+1。我的想法是错误的,我忽略了每个区间都是一个递推的过程。

比如:aaa,a是一个回文串则外面2个aa只要判断是否相等就可以了就可以。如果区间两头是相等的,则要加上dp[j+1][i-1]+1,因为首尾是可以组成一个回文子串的,而且首尾可以与中间任何一个回文子串组成新的回文子。

其结果要取余,利用来了 dp[i][j]=(dp[i][j]+10007)%10007;

Palindrome subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)
Total Submission(s): 2246    Accepted Submission(s): 895

Problem Description
In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>.
(http://en.wikipedia.org/wiki/Subsequence)

Given a string S, your task is to find out how many different subsequence of S is palindrome. Note that for any two subsequence X = <Sx1, Sx2, ..., Sxk> and Y = <Sy1, Sy2, ..., Syk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also two subsequences with different length should be considered different.

 
Input
The first line contains only one integer T (T<=50), which is the number of test cases. Each test case contains a string S, the length of S is not greater than 1000 and only contains lowercase letters.
 
Output
For each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.
 
Sample Input
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
 
Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960
 
Source
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int dp[][];
int main()
{
int t,k,i,j,g,len,r,p,q,e,m,b;
char str[];
cin>>q;
for(r=;r<=q;r++)
{
memset(dp,,sizeof(dp));
scanf("%s",str);
len=strlen(str);
for(i=;i<len;i++)
dp[i][i]=;//初始化,单个字符肯定是一个回文子串
for(k=;k<len;k++)
{
for(i=;i<len-k;i++)
{
j=i+k;
dp[i][j]=dp[i+][j]+dp[i][j-]-dp[i+][j-];////如果区间两头是相等的,则要加上dp[j+1][i-1]+1,
//因为首尾是可以组成一个回文子串的,而且首尾可以与中间任何一个回文子串组成新的回文子串
if(str[i]==str[j])
dp[i][j]+=dp[i+][j-]+;
dp[i][j]=(dp[i][j]+)%;//结果取余。
}
} printf("Case %d: %d\n",r,dp[][len-]);
}
return ;
}
/*
4
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960
*/
 

HDU-4632 http://acm.hdu.edu.cn/showproblem.php?pid=4632的更多相关文章

  1. HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...

  2. KMP(http://acm.hdu.edu.cn/showproblem.php?pid=1711)

    http://acm.hdu.edu.cn/showproblem.php?pid=1711 #include<stdio.h> #include<math.h> #inclu ...

  3. 待补 http://acm.hdu.edu.cn/showproblem.php?pid=6602

    http://acm.hdu.edu.cn/showproblem.php?pid=6602 终于能够看懂的题解: https://blog.csdn.net/qq_40871466/article/ ...

  4. HDU-1257 导弹拦截系统 http://acm.hdu.edu.cn/showproblem.php?pid=1257

    Problem Description 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高 ...

  5. http://acm.hdu.edu.cn/showproblem.php?pid=2579

    #include<stdio.h> #include<string.h> #include<queue> #define N 110 int m, n, k, x1 ...

  6. KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

    riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)但:aaaa与aa拼接后aa ...

  7. HDU 2544 最短路 http://acm.hdu.edu.cn/showproblem.php?pid=2544

    //代码: //方法1:Dijkstra's Algorithm #include<stdio.h> #include<math.h> #include<string.h ...

  8. HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973

    #include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #inc ...

  9. HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

随机推荐

  1. [Browsable(false)]

    1.c#方法上面的[Browsable(false)]是干吗用的? 答案:标明此对象不可被浏览,这样它就不会出现在设计器的属性窗口里了 看如下代码: /// <include file='Asp ...

  2. cojs 安科赛斯特 题解报告

    QAQ 从IOI搬了一道题目过来 官方题解貌似理论上没有我的做法优,我交到BZOJ上也跑的飞快 结果自己造了个数据把自己卡成了4s多,真是忧桑的故事 不过貌似原题是交互题,并不能离线 说说我的做法吧 ...

  3. net中使用母版页

    .net中使用母版页的优点 母版页提供了开发人员已通过传统方式创建的功能,这些传统方式包括重复复制现有代码.文本和控件元素:使用框架集:对通用元素使用包含文件:使用 ASP.NET 用户控件等.母版页 ...

  4. 创业草堂之十:换位思考:假如你是VC

    前一回“创业草堂”的讲义是一份VC的Termsheet,这里面浓缩着VC投资关键性的理念. 今天来对Termsheet做些实质性的讲解.最有效的学习方法是创业者你亲自来扮演一回VC的角色,学习如何从V ...

  5. ARM CPU与Intel x86 CPU性能比较

    Qualcomm ARM CPU与Intel x86 CPU性能比较 随着移动互联网时代的到来,Qualcomm(高通).Texas Instruments(德州仪器)等基于ARM架构的CPU受到越来 ...

  6. Java入门到精通——基础篇之多线程实现简单的PV操作的进程同步

    Java入门到精通——基础篇之多线程实现简单的PV操作的进程同步 一.概述     PV操作是对信号量进行的操作.     进程同步是指在并发进程之间存在一种制约关系,一个进程的执行依赖另一个进程的消 ...

  7. 基于Jws的WebService项目

    基于Jws的WebService项目   1.服务器端建立 1.1.创建接口 [java] view plaincopy @WebService  public interface IWebServi ...

  8. 机器学习 —— 概率图模型(Homework: MCMC)

    除了精确推理之外,我们还有非精确推理的手段来对概率图单个变量的分布进行求解.在很多情况下,概率图无法简化成团树,或者简化成团树后单个团中随机变量数目较多,会导致团树标定的效率低下.以图像分割为例,如果 ...

  9. Map集合案例

    1.获取字符串中每一个字母出现的次数. 比如"aababcabcdabcde",结果为:a(5)b(4)c(3)d(2)e(1) 分析如下: package mapexercise ...

  10. Java API —— Random类

    1.Random类概述         此类用于产生随机数         如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...