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. gpio子系统和pinctrl子系统(下)

    情景分析 打算从两个角度来情景分析,先从bsp驱动工程师的角度,然后是驱动工程师的角度,下面以三星s3c6410 Pinctrl-samsung.c为例看看pinctrl输入参数的初始化过程(最开始的 ...

  2. 3.FireDAC组件快照

    TFDManager 连接定义和Connect连接管理  TFDConnection 数据库连接组件,支持三种连接方式:1.持久定义(有一个唯一名称和一个配置文件,可以由FDManager管理) 例: ...

  3. 【2017 Multi-University Training Contest - Team 1】小结

    啊人生第一次打多校被虐 紧随yql的脚步做题. 1001: 可以发现我们平时表示的数都是$x*log_{10}{10}$,所以类似于做一个换底公式就可以了. -1是一个烟雾弹,因为小学生都知道2^n不 ...

  4. ansible安装和配置

    一.安装ansible准备 //安装准备 .两台机器 172.7.15.106 172.7.15.111 .设置hostname以及hosts 172.7.15.106 web9.lulu.com 1 ...

  5. jstorm系列-1:入门

    一.             Storm整体介绍 Storm 是一个类似Hadoop MapReduce的系统, 用户按照指定的接口实现一个任务,然后将这个任务递交给JStorm系统,Jstorm将这 ...

  6. Restful Framework (三)

    目录 一.版本 二.解析器 三.序列化 四.请求数据验证 一.版本 回到顶部 程序也来越大时,可能通过版本不同做不同的处理 没用rest_framework之前,我们可以通过以下这样的方式去获取. c ...

  7. 深度学习开源工具——caffe介绍

    本页是转载caffe的一个介绍,之前的页面图都down了,更新一下. 目录 简介 要点记录 提问 总结 简介 报告时间是北京时间 12月14日 凌晨一点到两点,主讲人是 Caffe 团队的核心之一 E ...

  8. 【转】Debug 运行正常,Release版本不能正常运行

    http://blog.csdn.net/ruifangcui7758/archive/2010/10/18/5948611.aspx引言 如果在您的开发过程中遇到了常见的错误,或许您的Release ...

  9. 【转载】关于Python的Mixin模式

    本博按: mixin是看起来是多继承的一种,但是,这种继承并不作为父类存在,而是增加功能到子类中. 像C或C++这类语言都支持多重继承,一个子类可以有多个父类,这样的设计常被人诟病.因为继承应该是个” ...

  10. 通过百度地图API获取经纬度以及两点间距离

    package com.baidumap; import java.io.BufferedReader; import java.io.IOException; import java.io.Inpu ...