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 = <S
x1, S
x2, ..., S
xk> and Y = <S
y1, S
y2, ..., S
yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S
xi
 = S
yi. 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
 

题意:找到一个字符串里又多少个回文串子序列,要注意子序列的定义哦!

思路:这几天做了几道区间DP,这算是比较简单的一道区间DP了,dp数组仍然是储存区间内的回文串数,然后推导出状态即可

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int mod = 10007;
char str[1005];
int dp[1005][1005]; int main()
{
int t,i,j,k,len,cas = 1;
scanf("%d",&t);
while(t--)
{
scanf("%s",str);
len = strlen(str);
for(i = 0; i<len; i++)
dp[i][i] = 1;//单个字符肯定是一个回文子串
for(i = 1; i<len; i++)
{
for(j = i-1; j>=0; j--)
{
dp[j][i] = (dp[j+1][i]+dp[j][i-1]-dp[j+1][i-1]+mod)%mod;//之前没有加mod,wa了,j~i的区间的回文数是j+1~i与j~i-1区间回文数的和,但是要注意这里会有重复的
if(str[i] == str[j])
dp[j][i] = (dp[j][i]+dp[j+1][i-1]+1+mod)%mod;//如果区间两头是相等的,则要加上dp[j+1][i-1]+1,因为首尾是可以组成一个回文子串的,而且首尾可以与中间任何一个回文子串组成新的回文子串
}
}
printf("Case %d: %d\n",cas++,dp[0][len-1]);
} return 0;
}

HDU4632:Palindrome subsequence(区间DP)的更多相关文章

  1. hdu4632 Palindrome subsequence (区间dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4632 题意:求回文串子串的的个数. 思路:看转移方程就能理解了. dp[i][j] 表示区 ...

  2. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  3. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

  4. [HDU4362] Palindrome subsequence (区间DP)

    题目链接 题目大意 给你几个字符串 (1<len(s)<1000) ,要你求每个字符串的回文序列个数.对于10008取模. Solution 区间DP. 比较典型的例题. 状态定义: 令 ...

  5. hdu4632 Palindrome subsequence ——区间动态规划

    link:http://acm.hdu.edu.cn/showproblem.php?pid=4632 refer to: o(╯□╰)o……明明百度找的题解,然后后来就找不到我看的那份了,这位哥们对 ...

  6. HDU 4632 Palindrome subsequence(区间DP求回文子序列数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...

  7. hdu4632 Palindrome subsequence 回文子序列个数 区间dp

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  8. Hdu4632 Palindrome subsequence 2017-01-16 11:14 51人阅读 评论(0) 收藏

    Palindrome subsequence Problem Description In mathematics, a subsequence is a sequence that can be d ...

  9. HDU4632 Palindrome subsequence

    标签(空格分隔): 区间qp Palindrome subsequence \[求一个string的 回文子序列 的个数 \] 少废话,上代码. #include<bits/stdc++.h&g ...

随机推荐

  1. nginx + tomcat

    http://blog.csdn.net/sun305355024sun/article/details/8620996

  2. write_chip,read_chip

    int write_chip(UINT32 addr, UINT32 data) { if(0 == fpgaRWMode) /* localbus mode */ { UINT16 datah, d ...

  3. 关于广义后缀树(多串SAM)的总结

    之前我们给的SAM的例题,基本上是一个串建SAM的就能做的 如果要建多个串的SAM应该怎么做呢 首先看题,bzoj2780 我一开始的想法是SA以前的弄法,把串拼起来,中间加分隔符做SAM 这题确实可 ...

  4. Office启动加载vs。。。项

    PowerPoint: 选项->加载项->Chinese Translation Addin->管理[COM加载项]转到->取消Chinese Translation Addi ...

  5. apache开源项目--solr

    solr 名称来源 Search On Lucene Replication solr 基本概况 Apache Solr (读音: SOLer) 是一个开源的搜索服务器.Solr 使用 Java 语言 ...

  6. Java [leetcode 15] 3Sum

    问题描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  7. Java [leetcode 31]Next Permutation

    题目描述: Implement next permutation, which rearranges numbers into the lexicographically next greater p ...

  8. 在Linux运行期间升级Linux系统(Uboot+kernel+Rootfs)

    版本:v1.2   Crifan Li 摘要 本文主要介绍了如何在嵌入式Linux系统运行的时候,进行升级整个Linux系统,包括uboot,kernel和rootfs.以及简介Linux中的已有的通 ...

  9. Asp.Net 高性能框架 SqlSugar.ORM 2.3

    一.前言 SqlSugar从去年到现在已经一年了,版本从1.0升到了现在的2.3 ,这是一个稳定版本 ,有数家公司已经项目上线,在这里我将SqlSugar的功能重新整理成一篇新的贴子,希望大家喜欢. ...

  10. CF GYM 100703M It's complicate

    题意:龙要做茶,需要n种原料,给出他有的原料个数,和每份茶需要的原料个数,和每种原料的价格,要求做整数份茶,把他之前有的原料用完最少要花多少钱. 解法:水题. 代码: #include<stdi ...