Back character sequence

Source: Internet
Author: User

Reprint http://blog.csdn.net/u014800748/article/details/45148441

1, problem description

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

5abaabcbaddabcba12111112351121cccccccfdadfa

Sample output

Case #1:5Case #2:277Case #3:1333Case #4:127Case #5:17

2. Analysis

To define the number of palindrome strings between the string [I,j] for DP (I,J), you can get the following recursion:

DP (I,J) =DP (i+1,j) +DP (i,j-1) +res (j-i>0) where res is to be discussed:

(1) If S[I]==S[J] then the middle overlap partial DP (I+1,J-1) does not need to subtract, only need to add an empty string case, namely Res=1;

Conversely, according to the principle of tolerance, it is necessary to subtract the part of the middle overlapping calculation, namely RES=-DP (I+1,J-1).

When I==j, there is only one character, that is 1, so it is not difficult to find the final answer by recursion, the final answer is DP (1,LEN+1). Len represents the length of the input string, starting from 1.

1#include <iostream>2#include <algorithm>3#include <string>4#include <sstream>5#include <Set>6#include <vector>7#include <stack>8#include <map>9#include <queue>Ten#include <deque> One#include <cstdlib> A#include <cstdio> -#include <cstring> -#include <cmath> the#include <ctime> -#include <functional> - using namespacestd; -  + #defineMoD 100007 - intdp[1005][1005]; + Chars[1005]; A  at intFintLintR) - { -     if(Dp[l][r]! =-1)  -         returnDp[l][r]; -     if(L >R) -         returnDP[L][R] =0; in     if(L = =R) -         returnDP[L][R] =1; to     int& ret =Dp[l][r]; +  -ret = (F (l +1, R) + F (L, R-1)) %MoD; the  *     if(S[l] = =S[r]) $++ret;//when the end is equal, plus a middle empty casePanax Notoginseng     Else  -RET-= F (L +1, R-1);//Otherwise, subtract overlapping parts the  +RET = (ret + MoD)%MoD; A     returnret; the } +  - intMain () $ { $      -     intCAS =0, T; -CIN >>T; the      while(t--)  -     {WuyiMemset (DP,-1,sizeof(DP)); theCin >> (s+1); -         intAns = F (1, strlen (S +1)); Wuprintf"Case #%d:%d\n", ++cas, ans); -     } About     return 0; $}

Back character sequence

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.