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. [CLR via C#]18. Attribute

    attribute可以说是Microsoft .NET Framework提出的最具创意的技术之一了.利用attribute,可以声明性的为自己的代码构造添加注解,从而实现一些特殊的功能.attrib ...

  2. C#的Raw Socket实现网络封包监视

    同Winsock1相比,Winsock2最明显的就是支持了Raw Socket套接字类型,使用Raw Socket,可把网卡设置成混杂模式,在这种模式下,我们可以收到网络上的IP包,当然包括目的不是本 ...

  3. 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之文件系统的新特 ...

  4. u-boot中nandflash初始化流程分析(转)

    u-boot中nandflash初始化流程分析(转) 原文地址http://zhuairlunjj.blog.163.com/blog/static/80050945201092011249136/ ...

  5. oop典型应用:实体类

    1.什么是实体类 简单地说就是描述一个业务实体的“类”,业务实体直观一点理解就是整个就是整个软件系统业务所涉及的对象. eg:MySchool系统中的班级,学生,年级等都是业务实体,“雷电”游戏中的飞 ...

  6. 关于IE中通过http-equiv="X-UA-Compatible指定文件兼容性模式

    .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier ...

  7. 浅析对象访问属性的"."和"[]"方法区别

    在JavaScript中通常使用”."运算符来存取对象的属性的值.或者使用[]作为一个关联数组来存取对象的属性.但是这两种方式有什么区别了? 例如,读取object中的property属性值 ...

  8. Android 多语言

    Android 多语言 在res文件上右击创建新的values文件 在strings文件中设置多语言 3.在layout文件中使用 @strings/key 引用相应资源

  9. .NET破解之google瓦片下载及拼接

    由于最近一些其他事忙,加之电脑显卡坏了,所以,好长一段时间没有更新博客了,感觉对不注关注我的朋友.从本文开始,博客更新频率将会大大降低,但每周都会更新的. 在上帝之眼论坛看到了新出来了一个google ...

  10. SharedPreference.Editor的apply和commit方法异同

    这两个方法的区别在于: 1. apply没有返回值而commit返回boolean表明修改是否提交成功 2. apply是将修改数据原子提交到内存, 而后异步真正提交到硬件磁盘, 而commit是同步 ...