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. 登录时显示403 Access Denied

    用户名及密码设置如下: 在tomcat安装目录\conf\tomcat-users.xml中的<tomcat-users>标签内设置: <role rolename="ma ...

  2. Python3使用csv模块csv.writer().writerow()保存csv文件,产生空行的问题

    问题:csv.writer().writerow()保存的csv文件,打开时每行后都多一行空行 解决方法:在open()内增加一个参数newline='' 即可   问题现象:   1.代码 with ...

  3. centos 系统下彻底删除mysql

    yum remove mysql mysql-server mysql-libs compat-mysql51 rm -rf /var/lib/mysql (删除数据库) rm /etc/my.cnf ...

  4. eclipse使用MAVEN打包可执行的jar包

    1.新建maven工程 注意勾选 随便填一下 建好之后工程目录如下 新建测试类与工具类,主类很简单 工具类也很简单,就是初始化了日志 maven依赖包也只有一个log4j的jar <depend ...

  5. python的协程和异步io【select|poll|epoll】

    协程又叫做微线程,协程是一种用户态的轻量级的线程,操作系统根本就不知道协程的存在,完全由用户来控制,协程拥有自己的的寄存器的上下文和栈,协程调度切换时,将寄存器上下文和栈保存到其他地方,在切换回来后, ...

  6. [leetcode]277. Find the Celebrity 找名人

    Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...

  7. URL传入带有%的参数的解决方法

    在参数中含有%时,需要将其转义为它的编码格式%25 ,后面跟对应字符的ASCII(16进制)码值 如我想要传入参数mm=75%到后台,就需要对%进行编码为%25,所以实际输入的参数是mm=75%25 ...

  8. 20-java 对象链表空没空呢

    写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用  == null  为假,也看不出, 看下长度? 好吧, size() = 1: 打印  null ,  那到底是不是空 啊, ...

  9. JSP的内置对象application、session、request、page的作用域

    1.application内置对象的类型名称为ServletContext. 2.session内置对象的类型名称为HttpSession. 3.application作用域:对应整个应用上下文. a ...

  10. URL统一资源定位符的组成

    URL:Uniform Resource Locator统一资源定位符 用于定位网络上我们需要访问的资源 组成:协议名称+域名+路径+资源的名称.如:https://img13.360img.com/ ...