Question 2: Return Character Sequence (interval DP), return dp

Source: Internet
Author: User

Question 2: Return Character Sequence (interval DP), return dp
Time Limit: 256 ms single point time limit: Ms memory limit: MB

Description

Returns the number of subsequences of input strings. The reversed Character Sequence is still the same as the original sequence. For example, in the string aba, the input sub-sequence is "a", "a", "aa", "B", and "aba". There are 5 sub-sequences in total. Subsequences with the same content and different locations calculate different subsequences.

Input

The first line is an integer T, indicating the number of data groups. Followed by T group data, each group of data is a string.

Output

Output a row of data in each group in the format of "Case # X: Y". X indicates the data number (starting from 1) and Y indicates the answer. The answer is modulo 100007.

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

Use dp [I] [j] to indicate the number of input strings in this segment, then dp [I] [j] = dp [I + 1] [j] + dp [I] [J-1], but dp [I + 1] [j] and dp [I] [J-1] may have a public part, so remove dp [I + 1] [J-1].

If str [I] = str [j], add dp [I + 1] [J-1] + 1.

Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iomanip>#include <algorithm>#include <queue>#define MOD 100007using namespace std;int dp[1005][1005];char str[1005];int main(){    int T,cnt=1;    cin>>T;    getchar();    while(T--)    {        gets(str);        int n=strlen(str);        int i,j;        for(i=0;i<n;i++)            dp[i][i]=1;        for(i=1;i<n;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;                if(str[i]==str[j])                    dp[j][i]=(dp[j][i]+dp[j+1][i-1]+1+MOD)%MOD;            }        }        printf("Case #%d: %d\n",cnt++,dp[0][n-1]);    }    return 0;}

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.