Palindrome subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)
Total Submission(s): 88    Accepted Submission(s): 26

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
 

写的递归形式的DP,

循环写可能会好一些。

各种卡常熟,要优化

/*
* Author:kuangbin
* 1001.cpp
*/ #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std;
const int MOD = ;
int dp[][];
int n;
char str[];
int solve(int l,int r)
{
if(l > r)return ;
if(l == r)return dp[l][r] = ;
if(dp[l][r] != -)return dp[l][r];
if(r == l+)
{
if(str[l] == str[r])
return dp[l][r] = ;
else return dp[l][r] = ;
}
dp[l][r] = solve(l+,r)+solve(l,r-);
if(dp[l][r] >= MOD)dp[l][r]-=MOD;
if(str[l]==str[r])
{
dp[l][r] ++;
if(dp[l][r] >= MOD)dp[l][r]-=MOD;
}
else
{
dp[l][r] -= solve(l+,r-);
if(dp[l][r]<)dp[l][r] += MOD;
}
return dp[l][r];
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int iCase = ;
scanf("%d",&T);
while(T--)
{
iCase++;
memset(dp,-,sizeof(dp));
scanf("%s",str);
n = strlen(str);
printf("Case %d: %d\n",iCase,solve(,n-));
}
return ;
}

HDU 4632 Palindrome subsequence (2013多校4 1001 DP)的更多相关文章

  1. HDU 4696 Answers (2013多校10,1001题 )

    Answers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  2. HDU 4666 Hyperspace (2013多校7 1001题 最远曼哈顿距离)

    Hyperspace Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Tota ...

  3. HDU 4632 Palindrome subsequence (区间DP)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  4. HDU 4632 Palindrome subsequence(区间dp)

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  5. hdu 4632 Palindrome subsequence

    http://acm.hdu.edu.cn/showproblem.php?pid=4632 简单DP 代码: #include<iostream> #include<cstdio& ...

  6. HDU 4632 Palindrome subsequence(区间DP求回文子序列数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...

  7. HDU 4632 Palindrome subsequence(DP)

    题目链接 做的我很无奈,当时思路很乱,慌乱之中,起了一个想法,可以做,但是需要优化.尼玛,思路跑偏了,自己挖个坑,封榜之后,才从坑里出来,过的队那么多,开始的时候过的那么快,应该就不是用这种扯淡方法做 ...

  8. HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)

    题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...

  9. HDU 4632 Palindrome subsequence (区间DP)

    题意 给定一个字符串,问有多少个回文子串(两个子串可以一样). 思路 注意到任意一个回文子序列收尾两个字符一定是相同的,于是可以区间dp,用dp[i][j]表示原字符串中[i,j]位置中出现的回文子序 ...

随机推荐

  1. 《Java编程思想》阅读笔记一

    Java编程思想 这是一个通过对<Java编程思想>(Think in java)第四版进行阅读同时对java内容查漏补缺的系列.一些基础的知识不会被罗列出来,这里只会列出一些程序员经常会 ...

  2. Centos7Yum安装配置指定版本nginx

    1.安装 rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm 2.启 ...

  3. Mysql 中的Text字段的范围

    mysql中text 最大长度为65,535(2的16次方–1)字符的TEXT列.如果你觉得text长度不够,可以选择 MEDIUMTEXT最大长度为16,777,215. LONGTEXT最大长度为 ...

  4. nginx: [emerg] unexpected “}”

    https://blog.csdn.net/gxdvip/article/details/46345129

  5. WebDriver自动化测试工具(2)---基本操作

    一.设置打开的浏览器大小/位置 driver.Manage().Window.Maximize(); //最大化 driver.Manage().Window.Position = , ); //设置 ...

  6. 求问asp.net mvc发布问题

    正常发布 浏览后如下

  7. 360杀毒导致的 VS 报扩展错误,请查看 ActiveLog.xml

    360杀毒将 TypeScript的 tsserver.js 列为木马,结果导致VS2017启动时,总是报错,将其加为信任即可解决.

  8. JavaScript性能优化【转载】

    你愿意为打开一个网页等待多长时间?我一秒也不愿意等.但是事实上大多数网站在响应速度方面都让人失望.现在越来越多的人开始建立自己的网站,博客,你的网页响应速度如何呢?在这篇文章中我们来介绍一下提高网页性 ...

  9. 浅谈对js原型的理解

    一.  在JavaScript中,一切皆对象,每个对象都有一个原型对象(prototype),而指向该原型对象的内部指针则是__proto__.当我们对对象进行for in 或者for of遍历时,就 ...

  10. CF 某套题 O :Grid (简单BFS)

    题意: 从左上角跳到右下角最少需要多少步,跳的规则为:可以向四个方向的任意一个方向跳当前格子中的步数,若跳不到右下角输出IMPOSSIBLE. 题解: BFS搜索,注意判断边界,标记. 代码: #in ...