Palindrome subsequence

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

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
 
大致题意是给定一个字符串,求这个字符串的回文子序列个数,最后的答案要%10007
 
题解:用dp[i][j]表示区间[i,j]回文子序列的个数
1、首先,初始化dp[i][j]=1;
 
2、然后预处理子结构,如果s[i]==s[i+1],则dp[i][i+1]=3(a,a,aa),否则 dp[i][i+1]=2  (a,b)
 
3、最后用区间DP处理:
 
如果s[i]==s[j],即子序列首尾相等,那么这段子序列区间内的所有回文子序列都可以和首尾元素再构成新的回文子序列,除此之外 s[i]和s[j]这两个元素也可以构成一个回文子序列
dp[i][j]=(dp[i+1][j]+dp[i][j-1]+1)%mod;
如果s[i]!=s[j],
dp[i][j]=(dp[i+1][j]+dp[i][j-1] dp[i+1][j-1]+mod)%mod;   (容斥原理,中间部分会计算两遍,所以要减去dp[i+1][j-1])
注意:做减法的时候,可能会有负数,为了使%不出现问题,我们需要先加mod再%mod
 

还要注意一下数据类型的问题,这道题目如过把dp[i][j]的类型直接定义为long long 会超时

1、因为long long 类型 运算没有int快 所以要改为int型 还有取模的数mod也必须要是int型 如果mod是long long的话也会超时.
 
2、能用int的就不要用long long,因为如果用了long long 就有可能超时;原来一直以为用ll不会爆范围,就总是用ll,现在发现了,一直用ll会爆时间,
尤其是在这种矩阵快速幂的题里,绝对要注意!!!
 
 
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#define ll long long
#define mod 10007
using namespace std;
int n,t;
int dp[][];
char s[];
ll min(ll a,ll b)
{
return a<b?a:b;
} int main()
{
cin>>n;
t=;
while(n--)
{
t++;
scanf("%s",s+);
int len=strlen(s+);
memset(dp,,sizeof(dp));
for(int i=;i<=len;i++)//初始化
dp[i][i]=;
for(int i=;i<len;i++)//预处理
{
if(s[i]==s[i+])
dp[i][i+]=;//a,a,aa
else
dp[i][i+]=;//a,b
}
for(int l=;l<=len;l++)
{
for(int i=;i+l-<=len;i++)
{
int j=i+l-;
if(j>len)
break;
if(s[i]==s[j])//如果首尾相等,则这段区间内的所有回文子序列都可以和首尾元素再构成新的回文子序列
dp[i][j]=(dp[i+][j]+dp[i][j-]+)%mod;
else
dp[i][j]=(dp[i+][j]+dp[i][j-]-dp[i+][j-]+mod)%mod; }
}
printf("Case %d: %d\n",t,dp[][len]%mod);
}
return ; }

hdu4632 Palindrome subsequence 回文子序列个数 区间dp的更多相关文章

  1. HDU 4745 Two Rabbits ★(最长回文子序列:区间DP)

    题意 在一个圆环串中找一个最长的子序列,并且这个子序列是轴对称的. 思路 从对称轴上一点出发,向两个方向运动可以正好满足题意,并且可以证明如果抽选择的子环不是对称的话,其一定不是最长的. 倍长原序列, ...

  2. 编程之美2015资格赛 题目2 : 回文字符序列 [ 区间dp ]

    传送门 题目2 : 回文字符序列 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定字符串,求它的回文子序列个数.回文子序列反转字符顺序后仍然与原序列相同.例如字符串ab ...

  3. HDU 4632 Palindrome subsequence & FJUT3681 回文子序列种类数(回文子序列个数/回文子序列种数 容斥 + 区间DP)题解

    题意1:问你一个串有几个不连续子序列(相同字母不同位置视为两个) 题意2:问你一个串有几种不连续子序列(相同字母不同位置视为一个,空串视为一个子序列) 思路1:由容斥可知当两个边界字母相同时 dp[i ...

  4. 【HDU】4632 Palindrome subsequence(回文子串的个数)

    思路:设dp[i][j] 为i到j内回文子串的个数.先枚举所有字符串区间.再依据容斥原理. 那么状态转移方程为   dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+ ...

  5. 【HIHOCODER 1323】回文字符串(区间DP)

    描述 给定一个字符串 S ,最少需要几次增删改操作可以把 S 变成一个回文字符串? 一次操作可以在任意位置插入一个字符,或者删除任意一个字符,或者把任意一个字符修改成任意其他字符. 输入 字符串 S. ...

  6. 【51nod 1092】 回文字符串(区间DP)

    回文串是指aba.abba.cccbccc.aaaa这种左右对称的字符串.每个字符串都可以通过向中间添加一些字符,使之变为回文字符串. 例如:abbc 添加2个字符可以变为 acbbca,也可以添加3 ...

  7. 便宜的回文串(区间DP)

    题目链接:便宜的回文串 这道题刚开始其实还是没有思路的.没办法,只能看题解了... 其实我们在思考问题时,考虑到一段串增或减时会改变它的长度,所以转移时会麻烦... 但其实不用考虑那么多的问题,我们只 ...

  8. 合并回文子串(区间dp)

    链接:https://ac.nowcoder.com/acm/problem/13230来源:牛客网 题目描述 输入两个字符串A和B,合并成一个串C,属于A和B的字符在C中顺序保持不变.如" ...

  9. hihocoder1323 回文字符串(区间dp)

    https://hihocoder.com/problemset/problem/1323 刚开始真没看出来这是一道dp题.. dp[i][j]表示i~j段修改成回文串所需的最少操作次数.然后根据s[ ...

随机推荐

  1. Android开发_*.R文件无法自动生成

    问题描述:             今天是我决定专注Android开发的第一天,我在网上下载了一个数独游戏的源码,准备开始着手学习.在导入之后出现Java文件中import *.R文件报错,在gen目 ...

  2. popupwindow使用之异常:unable to add window -- token null is not valid

    使用popwindow中又碰到一个白痴问题,在此留作纪念,希望对大家有帮助 popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛 void an ...

  3. C# WebApi的controller中如何存取session

    在MVC以后,Session方式可能已经不太常用,但偶尔还是会用到,比如页面验证码之类的.例如登录页面使用的验证码通过Controller提供一个View来实现,可以使用Session来存储这个值.但 ...

  4. 手写MQ框架(四)-使用netty改造梳理

    一.背景 书接上文手写MQ框架(三)-客户端实现,前面通过web的形式实现了mq的服务端和客户端,现在计划使用netty来改造一下.前段时间学习了一下netty的使用(https://www.w3cs ...

  5. 嵊州普及Day5T2

    题意:将(w,h)的纸条折成(W,H),最少需几步. 思路:横竖互不干扰,然后最多可折int型一半,拿个函数判断两次比较即可,然后折不了的条件是需要的矩形大于给的矩形. 见代码: #include&l ...

  6. Python 开篇

    一.Linux基础 - 计算机以及日后我们开发的程序防止的服务器的简单操作 二.Python开发 http://www.cnblogs.com/wupeiqi/articles/5433893.htm ...

  7. zookeeper加Kafka集群配置

    官方 https://zookeeper.apache.org/doc/r3.5.6/zookeeperStarted.html#sc_Prerequisites https://www.cnblog ...

  8. CrossOriginFilter

    当使用jQuery Ajax post请求时可能会遇到类似这样的错误提示 XMLHttpRequest cannot oad http://xxxxxx. Origin http://xxxxxx i ...

  9. python2学习------基础语法1 (变量、分支语句、循环语句、字符串操作)

    1.变量类型 Numbers(数字):int,float,long String(字符串) List(列表) tuple(元组) dict(字典) bool(布尔):True,False # 删除变量 ...

  10. vld扩展

    PHP代码的执行实际上是在执行代码解析后的各种opcode.通过vld扩展可以很方便地看到执行过程中的opcode. 一.安装vld扩展 git clone https://github.com/de ...