2015 Programming beauty Qualifying series number of palindrome subsequence

Source: Internet
Author: User

Time limit: 2000ms
Single point time limit: 1000ms
Memory Limit: 256MB
Describe
The number of palindrome sequences for a given string. The palindrome sequence reverses the character sequence and remains the same as the original sequence. For example, in the string aba, the palindrome sequence is "a", "a", "AA", "B", "ABA", a total of 5. Sub-sequences with different contents in the same location are counted as different subsequence.

Input
The first line is an integer t that represents the number of data groups. After that is the T-group data, each set of data is a row of strings.

Output
For each set of data output row, the format is "case #X: Y", X for the data number (starting from 1), and Y for the answer. Answer to 100007 modulo.

Data range
1≤t≤30
Small Data
String length ≤25
Big Data
String length ≤1000

Sample input
5
Aba
Abcbaddabcba
12111112351121
Ccccccc
Fdadfa
Sample output
Case #1:5
Case #2:277
Case #3:1333
Case #4:127
Case #5:17

Analysis:
1. Interval type DP, first of all, the state equation: if I to j this interval total number of palindrome subsequence, two cases ① when s[i] = = S[j], such as "a....a",d[i][j] = d[i][j-1] + d[i+1][j] + 1 (from d[i][j] = 2*d[i+1][j-1] + d[i][j-1] + d[i+1][j] + 1-2*d[i+1][j-1] simplification. Explanation: Known i+1 to J-1 have d[i+1][j-1] palindrome subsequence, and s[i] = = S[j], then can be with the middle (string i+1 to J-1) known palindrome sub-sequence and then constitute D[i+1][j-1] palindrome sub-sequence, plus the original middle string contains the palindrome sub-sequence altogether 2*d [I+1] [J-1], two a combination of "AA" is also a palindrome, so add 1, and then calculate the left and right side of a and the middle string of the palindrome sub-sequence, but this time is not over, note: In the calculation of the two sides of a and the middle string, and then two times the middle string contains the palindrome subsequence, so in minus 2*d[i+ 1][j-1] ② when s[i]! = s[j],d[i][j] = d[i][j-1]+d[i+1][j]-d[i+1][j-1]. This self should also be able to analyze, and similar to the above.
2. when the interval I to J is used, D[i+1][j-1], d[i][j-1], d[i+1][j]. This is why the interval DP generally starts from a smaller interval, and then does not widen the interval. Now the interval (i, j) used by the sub-range, before all have been finished, so or that sentence, direct use is good.
The opinion of that place is different to the welcome point, learn from each other!!

#include <iostream>#include<cstdio>#include<string.h>#include<cstring>using namespacestd;Const intMOD =100007;intT, Len, d[1010][1010];Chars[1010];voiddp () { for(inti =1; i < Len; i++)    {         for(intj =0; J < (Len-i); J + +)        {            if(S[j] = = s[j+i]) d[j][i+J] = (d[j][i+j-1] + d[j+1][I+J] +1) %MOD; ElseD[j][i+J] = (d[j][i+j-1] + d[j+1][I+J]-d[j+1][i+j-1] + MOD)%MOD; }    }}intMain () {CIN>>T;  for(intQ =1; Q <= t; q++) {memset (s),0,sizeof(s)); memset (d,0,sizeof(d)); CIN>>s; Len=strlen (s);  for(inti =0; i < Len; i++) D[i][i] =1;        DP (); printf ("Case #%d:%d\n", Q, d[0][len-1] %MOD); }    return 0;}
View Code

2015 Programming beauty Qualifying series number of palindrome subsequence

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.