Palindrome subsequence

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

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
 
状态方程dp[i][j] = dp[i+1][j]+dp[i][j-1] - dp[i+1][j-1]; 如果s[i] ==s[j] , dp[i][j]还要加上dp[i+1][j-1]+1; 
这道题WA了很惨,自己做题太少,对于有 -号再求余的一定要考虑是否有可能得出负数,加上mod之后可以保证是正数 dp[i][j] = (dp[i+1][j]+dp[i][j-1] - dp[i+1][j-1] +mod)%mod;
 
 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = ;
const int mod = ;
int _, n, dp[N][N], cas=;
char s[N]; void solve()
{
scanf("%s", s+);
n = strlen(s+);
memset(dp, , sizeof(dp));
for(int k=; k<n; k++)
{
for(int i=; i+k<=n; i++)
{
int t = i+k;
dp[i][t] = (dp[i+][t] + dp[i][t-] - dp[i+][t-] + mod) % mod;/////////注意
if(s[i]==s[t]) dp[i][t] += dp[i+][t-] + ;
dp[i][t] %= mod;
}
}
printf("Case %d: %d\n", cas++, dp[][n]);
} int main()
{
scanf("%d", &_);
while(_--) solve();
return ;
}
 

Palindrome subsequence的更多相关文章

  1. HDU 4632 Palindrome subsequence (区间DP)

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

  2. 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 ...

  3. HDU Palindrome subsequence(区间DP)

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

  4. HDU 4632 Palindrome subsequence (2013多校4 1001 DP)

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

  5. HDU 4632 Palindrome subsequence(区间dp)

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

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

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

  7. HDU4632 Palindrome subsequence

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

  8. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  9. Longest palindrome subsequence

    A palindrome is a nonempty string over some alphabet that reads the same forwardand backward. Exampl ...

随机推荐

  1. Ajax学习资源大全[本来是转载的,但是现在我增加了很多]

    本欲放转载区,但是这样一文章放那里基本是没有用的,帮助不了任何人!所以放新手了!! 我一般非经典或者自己用不上不转载,所以如果你不幸看见了的话,恰恰你又对AJAX有兴趣的话不防看下,也许对你有用的!! ...

  2. JavaScript跨域总结与解决办法

    什么是跨域 1.document.domain+iframe的设置 2.动态创建script 3.利用iframe和location.hash 4.window.name实现的跨域数据传输 5.使用H ...

  3. 用Perl编写Apache模块

    前言 Apache被许多大流量网站所嫌弃,但很多企业级的场景则更为适用. Apache httpd 从 2.0 之后,已经不仅仅局限于一个 http 的服务器,更是一个完善而强大.灵活而健壮且容易扩展 ...

  4. IOC容器中bean的生命周期

    一.Bean的生命周期 Spring IOC容器可以管理Bean的生命周期,允许在Bean生命周期的特定点执行定制的任务. Spring IOC容器对Bean的生命周期进行管理的过程如下: (1).通 ...

  5. 启动Tomcat出现“Bad version number in .class file (unable to load class XXX)”解决

    转载自:http://blog.csdn.net/justdb/article/details/8067887 主要是jdk版本的不搭配 保证tomcat 的jdk版本 项目的jdk版本 还有就是编译 ...

  6. jQuery对复选框(checkbox)的全选,全不选,反选等的操作

    效果截图: HTML代码: <body><ul id="list"> <li><label><input type=" ...

  7. 浅谈React受控与非受控组件

    背景 React内部分别使用了props, state来区分组件的属性和状态.props用来定义组件外部传进来的属性, 属于那种经过外部定义之后, 组件内部就无法改变.而state维持组件内部的状态更 ...

  8. https和http

    今天登网站的时候用https登的,没登上去用http找到网站了,于是就去百度了下他俩的区别简单的画了俩图.

  9. Ubuntu下修改system.img 解包system.img、打包system.img

    一.准备工作:解压解打包工具,得到三个文件:make_ext4fs.mkuserimg.sh.simg2img,把它们跟要修改的 .img.ext4(或.img)文件放置到同一个目录下 二.转换源文件 ...

  10. 解析plist文件(字典里包着数组,数组中又包含字典)

    #import "RootTableViewController.h" #import "City.h" @interface RootTableViewCon ...