hdu4632Palindrome subsequence (求迴文數,區間DP)

來源:互聯網
上載者:User

標籤:dp

Problem DescriptionIn 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 sequenceis a subsequence of . (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 =and Y = , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if S[sub]xi[/sub] = S[sub]yi[/sub]. Also two subsequences with different length should be considered different.
InputThe 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.
OutputFor each test case, output the case number first, then output the number of different subsequence of the given string, the answer should be module 10007.
Sample Input
4aaaaaagoodafternooneveryonewelcometoooxxourproblems

Sample Output
Case 1: 1Case 2: 31Case 3: 421Case 4: 960
#include<stdio.h>#include<string.h>int t,dp[1005][1005],c=0;int main(){    char str[1005];    scanf("%d",&t);    while(t--)    {        scanf("%s",str);        int len=strlen(str);        memset(dp,0,sizeof(dp));        for(int i=0;i<len;i++)        dp[i][i]=1;        for(int r=1;r<len;r++)        for(int i=0;i<len-r;i++)        {            int j=i+r;            dp[i][j]=(dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;            if(str[i]==str[j])            dp[i][j]=(dp[i][j]+dp[i+1][j-1]+1)%10007;        }        printf("Case %d: %d\n",++c,dp[0][len-1]);    }}


hdu4632Palindrome subsequence (求迴文數,區間DP)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.