首页 > 代码库 > hdu4632Palindrome subsequence (求回文数,区间DP)
hdu4632Palindrome subsequence (求回文数,区间DP)
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 sequenceis a subsequence of . (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 =and Y = , 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[sub]xi[/sub] = S[sub]yi[/sub]. 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#include<stdio.h> #include<string.h> int t,dp[1005][1005],c=0; int main() { char str[1005]; scanf("%d",&t); while(t--) { scanf("%s",str); int len=strlen(str); memset(dp,0,sizeof(dp)); for(int i=0;i<len;i++) dp[i][i]=1; for(int r=1;r<len;r++) for(int i=0;i<len-r;i++) { int j=i+r; dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007; if(str[i]==str[j]) dp[i][j]=(dp[i][j]+dp[i+1][j-1]+1)%10007; } printf("Case %d: %d\n",++c,dp[0][len-1]); } }
hdu4632Palindrome subsequence (求回文数,区间DP)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。