Palindrome subsequence

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
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  6010 6009 6008 6007 6006 
 

—————————————————————————————————————————————————————————

题目的意思是统计出一个字符串里有多少个不同的回文串的子串,不同指的是位置不同内容可以一样

区间dp,dp[i][j]表示区间[i,j]有多少个不同的回文子串。

推出状态转移方程为:

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;

答案要mod10007 要注意的是 由于有减 mod是要加10007修正

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <string>
using namespace std; int dp[1005][1005];
char s[1005];
int main()
{
int T;
while(~scanf("%d",&T))
{
int q=1;
while(T--)
{
scanf("%s",s);
int n=strlen(s);
memset(dp,0,sizeof dp);
for(int j=0;j<n;j++)
{
for(int i=j;i>=0;i--)
{
dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;
if(s[i]==s[j])
dp[i][j]+=dp[i+1][j-1]+1;
dp[i][j]%=10007;
}
}
printf("Case %d: %d\n",q++,dp[0][n-1]);
}
}
return 0;
}

Hdu4632 Palindrome subsequence 2017-01-16 11:14 51人阅读 评论(0) 收藏的更多相关文章

  1. Curling 2.0 分类: 搜索 2015-08-09 11:14 3人阅读 评论(0) 收藏

    Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14289 Accepted: 5962 Descript ...

  2. Oracle垃圾数据清理相关问题 分类: Oracle 2015-08-06 11:14 12人阅读 评论(0) 收藏

    垃圾数据清理,简单的说,就是删除不需要的那些数据,释放存储空间 最常用的就是delete命令.truncate命令,甚至是删除表空间重建,具体操作都很简单,不是本文的重点 下面,总结几个垃圾数据清理常 ...

  3. HRBUST1315 火影忍者之~大战之后 2017-03-06 16:14 54人阅读 评论(0) 收藏

    火影忍者之-大战之后 经历了大战的木叶村现在急需重建,人手又少,所以需要尽可能多的接受外来的任务,以赚取报酬,重建村庄,假设你现在是木叶的一名高级忍者,有一大堆的任务等着你来做,但毕竟个人时间有限,所 ...

  4. Gora官方范例 分类: C_OHTERS 2015-01-29 16:14 632人阅读 评论(0) 收藏

    参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...

  5. ubuntu中安装samba 分类: linux 学习笔记 ubuntu 2015-07-07 16:14 46人阅读 评论(0) 收藏

    为了方便的和Windows之间进行交互,samba必不可少. 当然,他的安装使用也很简单: 安装: sudo apt-get install samba sudo apt-get install sm ...

  6. 用IBM WebSphere DataStage进行数据整合: 第 1 部分 分类: H2_ORACLE 2013-08-23 11:20 688人阅读 评论(0) 收藏

    转自:http://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0602zhoudp/ 引言 传统的数据整合方式需要大量的手工 ...

  7. MS SQLServer 批量附加数据库 分类: SQL Server 数据库 2015-07-13 11:12 30人阅读 评论(0) 收藏

    ************************************************************ * 标题:MS SQLServer 批量附加数据库 * 说明:请根据下面的注释 ...

  8. winform timespan 两个时间的间隔(差) 分类: WinForm 2014-04-15 10:14 419人阅读 评论(0) 收藏

    TimeSpan 结构  表示一个时间间隔. 先举一个小例子:(计算两个日期相差的天数) 代码如下: DateTime dt = DateTime.Now.ToShortDateString(yyyy ...

  9. iOS纯代码手动适配 分类: ios技术 2015-05-04 17:14 239人阅读 评论(0) 收藏

    首先说下让自己的程序支持iPhone6和6+,第一种使用官方提供的launch screen.xib,这个直接看官方文档即可,这里不再多述:第二种方法是和之前iPhone5的类似,比较简单,为iPho ...

随机推荐

  1. 编写一个带有main函数的类,调用上面的汽车类,实例化奔驰、大众、丰田等不同品牌和型号,模拟开车过程:启动、加速、转弯、刹车、息火,实时显示速度。

    //程序入口    public static void main(String[] args) {        // TODO Auto-generated method stub         ...

  2. spring AOP 注解配置

    applicationContext-resource.xml: <?xml version="1.0" encoding="UTF-8"?>< ...

  3. https://developer.mozilla.org/

    Document/querySelector      https://developer.mozilla.org/zh-CN/docs/Web/API/Document/querySelector

  4. css:多个div在同一行显示

    使用float:left,也可以使用display : inline-block,可以使多个div在同一行显示. 示例如下: <div class="search_row"& ...

  5. tf.FIFOQueue()

    Tensorflow–tf.FIFOQueue详解描述tf.FIFOQueue根据先进先出(FIFO)的原则创建一个队列.队列是Tensorflow的一种数据结构,每个队列的元素都是包含一个或多个张量 ...

  6. Minimum Cost(最小费用最大流,好题)

    Minimum Cost http://poj.org/problem?id=2516 Time Limit: 4000MS   Memory Limit: 65536K Total Submissi ...

  7. vs2008 FileUpload 上传控件 Gridview传多个值

    拖fileupload控件,控件后加button1 lable1 lable2,还要在与本窗体同意目录下新建img文件夹protected void Button1_Click(object send ...

  8. NetworkStateReceiver的简单应用

    一.网络状态接收者是一个广播接收者当网络状态发生改变时会收到网络状态改变的广播 本例当网络状态改变时会进行相应处理 例如当断网时会发出通知点击通知后 会打开网络设置界面 代码如下: package c ...

  9. HttpApplicationState与HttpApplication

    HttpApplicationState 类的单个实例在客户端第一次从某个特定的 ASP.NET 应用程序虚拟目录中请求任何 URL 资源时创建.对于 Web 服务器上的每个 ASP.NET 应用程序 ...

  10. Web开发者工具下载

    登录微信公众号:https://mp.weixin.qq.com