HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
参考自博客:http://blog.csdn.net/u011498819/article/details/38356675
题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数。
简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就
知道了,若已经知道【0,i】,推【0,i+1】, 显然还要从i+1 处往回找,dp方程也简单:
dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][i-1])%10007; 减去中间一段重复的
if(s[i]==s[j])dp[j][i]=(dp[j][i]+dp[j+1][i-1]+1)%10007; 这里不忘记,新加入的和结尾构成的情况。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int dp[][];//dp[i][j] i~j之间的回文串数目
int main()
{
int t;
scanf("%d",&t);
for(int id=;id<=t;id++)
{
char s[];
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++)
{
for(int j=i-;j>=;j--)
{
//当有减法的时候,取模要加个模!否则负数!
dp[j][i]=(dp[j+][i]+dp[j][i-]-dp[j+][i-]+)%;//忘记模了。。。
if(s[i]==s[j])
dp[j][i]=(+dp[j+][i-]+dp[j][i])%;//忘记模了。。。
}
}
printf("Case %d: %d\n",id,dp[][len-]%);
}
return ;
}
HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)的更多相关文章
- HDU 4632 Palindrome subsequence(区间DP求回文子序列数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...
- HDU 4632 Palindrome subsequence (区间DP)
题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...
- CodeForces-245H:Queries for Number of Palindromes(3-14:区间DP||回文串)
Times:5000ms: Memory limit:262144 kB 给定字符串S(|S|<=5000),下标由1开始.然后Q个问题(Q<=1e6),对于每个问题,给定L,R,回答区间 ...
- [LeetCode] Palindrome Partitioning II 拆分回文串之二
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
- LightOJ - 1205:Palindromic Numbers (数位DP&回文串)
A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...
- HDU 4632 Palindrome subsequence (区间DP)
Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/ ...
- hdu4632 Palindrome subsequence (区间dp)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4632 题意:求回文串子串的的个数. 思路:看转移方程就能理解了. dp[i][j] 表示区 ...
- HDU4632:Palindrome subsequence(区间DP)
Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...
- poj 3280 Cheapest Palindrome ---(DP 回文串)
题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...
随机推荐
- C 简易基础开发框架 - simple c
引言 一个为 简单高效而生的 简易跨平台的 纯C开发框架. githup上源码 https://github.com/wangzhione/sconsole_project 请容我细说 s ...
- 删除字符串第一个byte
删除字符串第一个byte 一种方式: char * mag; char buff[1000]; char number; memcpy((char *)msg,buff,len); strnc ...
- Microsoft SqlServer2008技术内幕:T-Sql语言基础-读书笔记1
一.理论背景:关系模型,其数学理论是集合论和谓词逻辑. 1.集合论:集合定义是把我们直观或思维中确定的,相互间有明确区别的那些对象视为一个整体,这个整体就是集合. 2.谓词逻辑:谓词是判断对象是否有某 ...
- 'mysql.column_stats' doesn't exist and Table 'mysql.index_stats' doesn't exist
在生产库MariabDB中修改字段类型,提示如下错误:Table 'mysql.column_stats' doesn't existTable 'mysql.index_stats' doesn' ...
- Oracle Database Gateway 安装
在[Oracle HS (Heterogeneous Services)深入解析 及协同Gateway工作流程]一文中主要主要介绍了HS的工作原理,及其如何协同Gateway一起工作.那么了解Gate ...
- 图片模糊度判断程序(C++、opencv)
//#include<opencv2\opencv.hpp> //using namespace cv; #include <opencv2/core/core.hpp> #i ...
- ubuntu14.04建立交叉编译环境, 注意事项
ubuntu14.04建立交叉编译环境, 注意事项 ~$ arm-linux-gcc/opt/FriendlyARM/toolschain/4.4.3/bin/arm-linux-gcc: 15: e ...
- ASP.NET中Global.asax 文件是什么?
Global.asax 文件,有时候叫做 ASP.NET 应用程序文件,提供了一种在一个中心位置响应应用程序级或模块级事件的方法.你可以使用这个文件实现应用程序安全性以及其它一些任务.下面让我们详细看 ...
- Integer对象
数字格式的字符串转成基本数据类型的方法: 1:将该字符串封装成了Integer对象,并调用对象的方法intValue(); 2:使用Integer.parseInt(numstring):不用建立对象 ...
- 用vs2013编译lua源码方法
1.下载lua源码:lua-5.2.3.tar.gz,解压 2.用vs2013建立一个win32工程: 1)下载后解压到一个目录下,这里假设解压到 F:\lua-5.2.3 注意下载的版本,如果是 ...