HDU4632:Palindrome subsequence(区间DP)
(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.
a
aaaaa
goodafternooneveryone
welcometoooxxourproblems
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)的更多相关文章
- hdu4632 Palindrome subsequence (区间dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4632 题意:求回文串子串的的个数. 思路:看转移方程就能理解了. dp[i][j] 表示区 ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- HDU 4632 Palindrome subsequence (区间DP)
题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...
- [HDU4362] Palindrome subsequence (区间DP)
题目链接 题目大意 给你几个字符串 (1<len(s)<1000) ,要你求每个字符串的回文序列个数.对于10008取模. Solution 区间DP. 比较典型的例题. 状态定义: 令 ...
- hdu4632 Palindrome subsequence ——区间动态规划
link:http://acm.hdu.edu.cn/showproblem.php?pid=4632 refer to: o(╯□╰)o……明明百度找的题解,然后后来就找不到我看的那份了,这位哥们对 ...
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...
- hdu4632 Palindrome subsequence 回文子序列个数 区间dp
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- 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 ...
- HDU4632 Palindrome subsequence
标签(空格分隔): 区间qp Palindrome subsequence \[求一个string的 回文子序列 的个数 \] 少废话,上代码. #include<bits/stdc++.h&g ...
随机推荐
- Foreman--管理PuppetClient
一. 环境: 1. server: puppetmaster+activemq+foreman1.3 server1.xxx.com(10.8.1.201) 2. client: fedora 19 ...
- lib库依赖解决
当前环境之前是装过MySQL官方版本5.6.22,想测试Percona版本MySQL.启动Percona-MySQL报错. [root@dg7 support-files]# /etc/init.d/ ...
- asp mvc 路由
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Workflo ...
- poj1989
一道非常神奇的题目 var v:array[0..10010] of boolean; n,k,i,x,ans,s:longint; begin readln(n,k); fillchar(v,siz ...
- (转载) ExtJs大比拼JQuery:Dom文档操作
此次不生产水,做一次搬运工. http://www.cnblogs.com/lipan/archive/2011/12/07/2269815.html
- POJ 3683 Priest John's Busiest Day (2-SAT,常规)
题意: 一些人要在同一天进行婚礼,但是牧师只有1个,每一对夫妻都有一个时间范围[s , e]可供牧师选择,且起码要m分钟才主持完毕,但是要么就在 s 就开始,要么就主持到刚好 e 结束.因为人数太多了 ...
- shell判断条件是否存在
1. shell判断文件,目录是否存在或者具有权限 2. #!/bin/sh 3. 4. myPath="/var/log/httpd/" 5. myFile="/var ...
- 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符
// 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, c ...
- 白话spring依赖注入
Spring能有效地组织J2EE应用各层的对象.Action?Service?DAO?,都可在Spring的管理下有机地协调.运行.Spring将各层的对象以松耦合的方式组织在一起,对象与对象之间没有 ...
- InstallShield高级应用--检查是否安装ORACLE或SQL Server
InstallShield高级应用--检查是否安装ORACLE或SQL Server 实现原理:判断是否存在,是通过查找注册表是否含有相应标识来判断的. 注意:XP与WIN7系统注册表保存方式不一 ...