Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65535 K (Java/Others)
Total Submission(s): 2858 Accepted Submission(s): 1168
(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.
#include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> #define N 1005 using namespace std; int dp[1005][1005]; char str[1005]; int main() { int tcase; int k = 1; scanf("%d",&tcase); while(tcase--){ scanf("%s",str+1); int len = strlen(str+1); for(int i=1;i<=len;i++) dp[i][i]=1; for(int l=2;l<=len;l++){ for(int i=1;i<=len-l+1;i++){ int j=i+l-1; dp[i][j] = (dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;///子问题推出父问题(减掉重复子问题) ///有减号要加mod防止出现负数 if(str[i]==str[j]){ dp[i][j] = (dp[i][j]+dp[i+1][j-1]+1)%10007;///如果str[i]str[j]相等,那么会多出dp[i+1][j-1]+1个回文串 } } } printf("Case %d: %d\n",k++,dp[1][len]); } return 0; }