Palindrome subsequence

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

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
 
有两个点要学习
1. 对于计数类的区间dp 这个状态转移方程比较有意思
dp[i][j] = (dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + mod) % mod;
具体理解这个转移方程 自己枚举几个样例就知道了 至于怎么想到这个的
对于s[i]!=s[j]的情况 dp[i][j]所有情况是 dp[i][j-1],dp[i+1][j]这个两个状态的交集 (不得不说一下,由于是枚举所有的情况,这个理解可以从 求i~j这个区间的所有子序列个数归纳出来)
然后当s[i] == s[j]的时候(或者说我们之考虑 dp[i][j-1],dp[i+1][j]两个状态交集的时候),解是不全的 漏下的解是当j 可以和dp[i][j-1]或者 i可以和dp[i+1][j-1]的回文子串构成新的回文字串的情况 这两重情况去重以后 就可以写成
if(a[i] == a[j]) dp[i][j] = (dp[i][j] + dp[i+1][j-1] + 1) % mod;
2.就是。。 取模的时候 如果有减法。 一定要加上模数啊。。 负数wa死你
#include<cstdio>
#include<iostream>
#include<cstring> using namespace std;
const int maxn = 1005;
const int mod = 10007; char a[maxn];
int n,dp[maxn][maxn]; int solve(){
memset(dp,0,sizeof(dp));
for(int i=0;i<n;i++) dp[i][i]=1;
for(int i = n-2 ; i >= 0 ;i--)// 由于dp[i][j]依赖 dp[i+1] 所以枚举区间的时候,我们要从大到小枚举i
{
for(int j = i+1 ;j < n ; j++)
{
dp[i][j] = (dp[i+1][j] + dp[i][j-1] - dp[i+1][j-1] + mod) % mod;
if(a[i] == a[j]) dp[i][j] = (dp[i][j] + dp[i+1][j-1] + 1) % mod;
}
}
return dp[0][n-1];
} int main(){
int cas;
scanf("%d",&cas);
for(int T = 1 ; T <= cas; T++){
scanf("%s",a);
n = strlen(a);
printf("Case %d: %d\n",T,solve());
} return 0;
}

  

hdu 4632区间dp 回文字串计数问题的更多相关文章

  1. HDU 4632 区间DP 取模

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4632 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字 ...

  2. hdu 4632(区间dp)

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

  3. 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,回答区间 ...

  4. Hdu 3068 最长回文字串Manacher算法

    题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. hdu 4632区间 dp

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 用点容斥原理转移状态, dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+ ...

  6. LightOJ - 1205:Palindromic Numbers (数位DP&回文串)

    A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the sam ...

  7. P1435 回文字串

    P1435 回文字串 题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最 ...

  8. 求字符串的最长回文字串 O(n)

    昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...

  9. hihocoder 第一周 最长回文字串

    题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...

随机推荐

  1. Windows Server 2012R2 部署 Domain Controller

    1. Create a machine as Domain Controller; 2. Change DNS server address as 127.0.0.1; 3. Change Compu ...

  2. koa 项目实战(九)passport验证token

    1.安装模块 npm install koa-passport -D npm install passport-jwt -D 2.解析token 根目录/config/passport.js cons ...

  3. laravel-5.3(1) 路由配置

    第一步: 按照上一篇搭建好工程后可以看到框架默认的 welcome 默认视图: 一般的web 开发框架是MVC设计模式,那么我们现在创建自己的控制器和视图,CMD 进入到工程根目录执行 php art ...

  4. 手把手教你用蒲公英获取udid

    如果需要获取udid,但是拥有手机的测试用户身边没有mac电脑和xcode环境, 今天就分享一个快捷的在线获得udid的方法 利用蒲公英网站的获取udid功能 手机浏览器访问 http://www.p ...

  5. 前端js数据加密解密

    一.最简单的加密解密   函数escape()和unescape(); 二.base64加密 (1)introduction base64是网络上最常见的用于传输8bit字节码的编码方式之一,base ...

  6. HelloWorld入门代码

    A:定义类 B:写main方法 C:写输出语句 D:Java程序开发运行与工作原理 E:编译和运行程序 class HelloWorld { public static void main(Strin ...

  7. consul ocelot

    consul配置完成后 新建.netcoreapi项目, nuget安装ocelot 添加多个配置文件,.netcore中会自动合并为一个文件,global配置总的配置,其他为各个项目的配置 Serv ...

  8. Smarty section、foreach控制循环次数的实现详解

    <!--{ section name='i' loop=$a }--><!--{ if $smarty.section.i.index < 3 }--><!--{  ...

  9. 启动eclipse导致Tomcat的配置文件重置

    转: 启动eclipse导致Tomcat的配置文件重置 导入一个项目,需要在Tomcat的配置文件中配置JNDI数据源,需要修改Tomcat下的server.xml文件.但是当我们修改完后重启Tomc ...

  10. 阿里云安装 fastdfs 总结

    还要开放 23000  22122,添加进安全组