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


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.


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 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
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.

#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;}

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.