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. MYSQL企业常用架构与调优经验分享

    一.选择Percona Server.MariaDB还是MYSQL  mysql应用源码:http://www.jinhusns.com/Products/Download/?type=xcj 1.M ...

  2. VS用法总结

    工欲善其事,必先利其器,关于VS的“即时窗口”的使用: 打开“即时窗口”的快捷键是:Ctrl + Alt + I. 要在“即时窗口”中打印查看打印输出结果,必须按如下步骤设置:[工具]→[选项]→[调 ...

  3. Mongodb 语法,update,insert,delete,find

    ---恢复内容开始--- db.Users.update({OrganizationCode:"Global"},{$set:{OrganizationCode:"Fre ...

  4. 不要迷恋那些没技术含量的Linux发行版

    昨天悲剧了,重装系统,一个手贱点了替换原系统,分区全给删了,将近三天的工作成果没有了.

  5. 启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误

    启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误,如下 原因:eclipse的版本与jre或者jdk版本不 ...

  6. 阿里前CEO卫哲用自己10余年经历,倾诉B2B的三差、四率、两大坑

    今日(12 月 28 日),嘉御基金创始人.阿里巴巴(B2B)前 CEO 卫哲在第三届中国 B2B 电子商务大会上进行了"B2B 冬天里的春天"的主题分享.他提出中国 B2B 行业 ...

  7. C: const and static keywords

    原文:http://www.noxeos.com/2011/07/29/c-const-static-keywords/ C: const and static keywords Ok, once a ...

  8. 关于一个js连续赋值问题之我见(词略穷,见谅)

    前几天在搜索面试题时发现了这么一段代码,执行完后感觉完全不与所想的一样 var a = { n : 1 }; var b = a; a.x = a = {n : 2}; console.log(a.x ...

  9. 基本完成了一个SEGY扫描程序

    利用Seismic.NET编写了一个SEG-Y文件的扫描程序,可以自动判断道头字中主测线号.横测线号.X坐标和Y坐标的位置,自动快速扫描地震数据体中的拐点坐标.10GB多的数据体几十秒全部扫描完成! ...

  10. 用doxygen+graphviz自动化生成代码文档(附详细教程)

    一.引子 用这两个工具可以自动的遍历代码,并且产生代码文档,我们先来看看效果,然后放出这两个工具的下载地址. 二.工具的下载地址 doxygen:http://www.stack.nl/~dimitr ...