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 DescriptionIn mathematics, a subsequence was a sequence that can being derived from another sequence by deleting some elements without CH Anging the order of the remaining elements. For example, the sequence <a, B, d> are 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: subsequence X = <sx1, Sx2, ..., sxk> and Y = <sy1, Sy2, ..., syk>, if there exist an in Teger I (1<=i<=k) such that Xi! = Yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also subsequences with different length should be considered different.
InputThe first line contains only one integer T (t<=50), which is the number of the test cases. Each test case contains a string s, the length of S are not greater than and only contains lowercase letters.
OutputFor each test case, output the case number first, and then output the number of different subsequence of the given string, The answer should be module 10007.
Sample Input
4aaaaaagoodafternooneveryonewelcometoooxxourproblems
Sample Output
Case 1:1case 2:31case 3:421case 4:960
Sourcemulti-university Training Contest 4
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4632
The main topic: to a string, the number of all palindrome substring, the character position is different from the palindrome string
Topic Analysis: Very familiar with a DP, seems to be the programming of the United States qualifying game, the original is once more than the original problem ah ... We enumerate the intervals from left to right, for each interval from the outward inner count, Dp[i][j] indicates the number of palindrome substrings from I to j if s[i] = S[j], then dp[i][j] + = dp[i-1][j + 1] + 1, plus dp[i][j] = dp[i-1][j] + dp[i][j + 1]-dp[i-1][j + 1], here is the equivalent of a repulsion, remove the repetition of the part, the final answer is dp[len][1]
#include <cstdio> #include <cstring>int const MAX = 1005;int Const MOD = 10007;char S[max];int dp[max][max];int Main () { int T; scanf ("%d", &t); for (Int. CA = 1; CA <= T; ca++) { printf ("Case%d:", CA); memset (DP, 0, sizeof (DP)); scanf ("%s", S + 1); int len = strlen (s + 1); for (int i = 1; l <= Len; i++) {for (int j = i; j >= 1; j--) { if (i = = j) { Dp[i][j] = 1 ; Continue; } if (s[i] = = S[j]) Dp[i][j] + + dp[i-1][j + 1] + 1; DP[I][J] + = (5 * mod + dp[i-1][j] + dp[i][j + 1]-dp[i-1][j + 1])% MOD ; } printf ("%d\n", Dp[len][1]% MOD);} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 4632 palindrome subsequence (interval dp-tolerant theorem)