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. nstall neovim on Ubuntu 16.04

    https://neovim.io/ To install NeoVim on Ubuntu, run 1 2 3 sudo add-apt-repository ppa:neovim-ppa/sta ...

  2. Syncthing搭建

    现在貌似只有windows和linux比较号装. 安装 先从官网下载好Windows 32位版(我本本对应的系统版本)的Syncthing,解压后可以看到如下文件结构   Syncthing文件结构 ...

  3. nfs只能挂载为nobody的解决方法

    不得不承认centos6较centos5发生了很大的变化,在新部署的centos 6.4上又遇到nfs挂载的问题.问题现象是,在配置完nfs后,无论配置里指定的是何用户,挂载成功后显示的只能是nobo ...

  4. HTTP Error 500.0 - Internal Server Error错误代码0x80070002

    案例研究:AspNetInitClrHostFailureModule中的“HTTP错误500.0 - 内部服务器错误” 症状 当用户访问在Windows Server 2008 R2计算机上运行的A ...

  5. dreamwave基础

    WEBcs架构需要在客户段安装程序, 需要安装程序, 工作量会比较大, 需要安装和维护, 比如以后系统升级, 会很麻烦. 优点是一些业务逻辑可以在客户端, 可以减少服务器的一些压力, 客户端的界面操作 ...

  6. luoguP1090 合并果子 (贪心+优先队列)

    题目链接:https://www.luogu.org/problemnew/show/P1090 思路: 典型的贪心题,显然每次选择两个最小的堆合并最后耗费的体力最少,但每次合并之后都需要寻找最小的两 ...

  7. gridview 级联删除、dataset

    gridview编辑列(不使用控件绑定数据源)需要如下代码:<asp:GridView ID="GridView1" runat="server" Aut ...

  8. MySQL基本操作之命令行操作

    MySQL基础操作 MySQL基础操作--命令行操作

  9. 14-matlab排序

    冒泡和选择: clc; clear; n = 10; %输入数据的个数 suct = 0; %素数的个数 unsuct = 0;%非素数的个数 % a = [33 34 2 34 24 56 3 17 ...

  10. 10-string类的length()返回值一起的问题

    c++ string类length()(size())函数返回值–无符号数 首先,先来发现问题 string s = ""; for(int i = 0; i < s.len ...