HDU4632: Palindrome subsequence (interval DP)

Source: Internet
Author: User

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 shoshould be consider different even if Sxi = Syi. Also two subsequences with different length shocould 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 shocould be module 10007.
 


Sample Input
4
A
Aaaaa
Goodafternooneveryone
Welcometoooxxourproblems


Sample Output
Case 1: 1
Case 2: 31
Case 3: 421
Case 4: 960

 

Question: How many sub-sequences are in a string? Pay attention to the definition of sub-sequences!

Idea: Several Intervals of DP have been implemented over the past few days. This is a relatively simple interval of DP. The dp array is still the number of input strings in the storage interval, and the export status is enough.

 

 

 

# Include <stdio. h> # include <string. h ># include <algorithm> using namespace std; const int mod = 10007; char str [1005]; int dp [1005] [1005]; int main () {int t, i, j, k, len, cas = 1; scanf ("% d", & t); while (t --) {scanf ("% s", str ); len = strlen (str); for (I = 0; I <len; I ++) dp [I] [I] = 1; // a single character must be a response substring for (I = 1; I <len; I ++) {for (j = I-1; j> = 0; j --) {dp [j] [I] = (dp [j + 1] [I] + dp [j] [I-1]-dp [j + 1] [I-1] + mod) % mod; // not previously added Mod, wa, j ~ The number of replies in the I interval is j + 1 ~ I and j ~ The sum of the return numbers in the I-1 interval, but note that there will be repeated if (str [I] = str [j]) dp [j] [I] = (dp [j] [I] + dp [j + 1] [I-1] + 1 + mod) % mod; // if the two ends of the interval are equal, add dp [j + 1] [I-1] + 1, because the beginning and end can form a response string, and the beginning and end can form a new response string with any of the middle strings} printf ("Case % d: % d \ n", cas ++, dp [0] [len-1]);} return 0 ;}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.