Palindrome subsequence (区间DP)

Palindrome subsequence

HDU - 4632

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 = <S x1, S x2, ..., S xk> and Y = <Sy1, S y2, ..., S yk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S xi = S yi. Also two subsequences with different length should be considered different.

InputThe 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.OutputFor 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题意:  求字符串有多少回文子序列。(可以不连续)题解:  用dp[i][j]表示这一段里有多少个回文串,那首先dp[i][j]=dp[i+1][j]+dp[i][j-1],但是dp[i+1][j]和dp[i][j-1]可能有公共部分,即dp[i+1][j-1]是重复的,需要减去。如果s[l]==s[r],那么dp[l][r] = dp[l+1][r-1] + 1 。
 1 import java.util.Scanner;
 2
 3 public class Main {
 4     static int casen,mod = 10007;
 5     static int [][] dp ;
 6     static String s;
 7     public static void main(String[] args) {
 8         Scanner cin = new Scanner(System.in);
 9         casen = cin.nextInt();
10         int ca = 1;
11         while(casen-->0) {
12             s = cin.next();
13             dp = new int [s.length()+1][s.length()+1];
14             for(int i=0;i<s.length();i++) {
15                 dp[i][i] = 1;
16             }
17             for(int len=1;len<s.length();len++) {
18                 for(int l=0;l+len<s.length();l++) {
19                     int r = l+len;
20                     if(s.charAt(l)==s.charAt(r)) {
21                         dp[l][r] = (dp[l+1][r-1] + 1 + mod) % mod;
22                     }
23                     dp[l][r] = (dp[l][r] + dp[l][r-1] + dp[l+1][r] - dp[l+1][r-1] + mod) % mod;
24                 }
25             }
26             System.out.println("Case "+ ca++ + ": "+dp[0][s.length()-1]);
27         }
28     }
29 }
30  

原文地址:https://www.cnblogs.com/1013star/p/10409839.html

时间: 2024-11-09 17:41:11

Palindrome subsequence (区间DP)的相关文章

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

题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. 简单的区间dp,哎,以为很神奇的东西,其实也是dp,只是参数改为区间,没做过此类型的题,想不到用dp,以后就 知道了,若已经知道[0,i],推[0,i+1], 显然还要从i+1 处往回找,dp方程也简单: dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10007-dp[j+1][

HDU 4632 Palindrome subsequence (区间dp 容斥定理)

Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others) Total Submission(s): 2610    Accepted Submission(s): 1050 Problem Description In mathematics, a subsequence is a sequence that can be derived

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设dp[i][j]为[i,j]的回文子序列数,那么得到状态转移方程: dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+MOD)%MOD if(str[i]==str[j]) dp[i][j]+=dp[i-1][j+1]+1 代码: 1 #include<cst

POJ 3280 Cheapest Palindrome(区间DP求改成回文串的最小花费)

题目链接:http://poj.org/problem?id=3280 题目大意:给你一个字符串,你可以删除或者增加任意字符,对应有相应的花费,让你通过这些操作使得字符串变为回文串,求最小花费.解题思路:比较简单的区间DP,令dp[i][j]表示使[i,j]回文的最小花费.则得到状态转移方程: dp[i][j]=min(dp[i][j],min(add[str[i]-'a'],del[str[i]-'a'])+dp[i+1][j]); dp[i][j]=min(dp[i][j],min(add[

uva 10453 Make Palindrome (区间DP + 递归输出)

uva 10453 Make Palindrome 题目大意:给出一段字符串,要求求出最少加入几个字符(任意位置),可以让该字符串变成会问字符串,并输出修改以后的回文字符串. 解题思路:dp[i][j]代表了将该字符串从第i位到第j位变成回文字符串最少要添加的字符.当S[i]==S[j],dp[i][j]=dp[i+1][j?1]当S[i]!=S[j],dp[i][j]=min(dp[i+1][j],dp[i][j?1])+1,在DP的过程中记录对该区间的操作类型,最后递归输出. #includ

POJ 3280 Cheapest Palindrome(区间DP)

 Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6894   Accepted: 3344 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each co

lightoj-1044 - Palindrome Partitioning(区间dp)

1044 - Palindrome Partitioning PDF (English) Statistics ForumTime Limit: 1 second(s) Memory Limit: 32 MBA palindrome partition is the partitioning of a string such that each separate substring is a palindrome. For example, the string "ABACABA" c

poj3280--Cheapest Palindrome(区间dp)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6186   Accepted: 3014 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a

【POJ】3280 Cheapest Palindrome(区间dp)

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10943   Accepted: 5232 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow