zoukankan      html  css  js  c++  java
  • hdu 4632(区间dp)

    Palindrome subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)
    Total Submission(s): 2858    Accepted Submission(s): 1168


    Problem Description
    In 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 sequence <A, B, D> is a subsequence of <A, B, C, D, E, F>.
    (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 = <Sx1, Sx2, ..., Sxk> and Y = <Sy1, Sy2, ..., Syk> , if there exist an integer i (1<=i<=k) such that xi != yi, the subsequence X and Y should be consider different even if Sxi = Syi. Also two subsequences with different length should be considered different.
     
    Input
    The 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.
     
    Output
    For 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
    4 a aaaaa goodafternooneveryone welcometoooxxourproblems
     
    Sample Output
    Case 1: 1 Case 2: 31 Case 3: 421 Case 4: 960
     
     
    题意:找出一个串中所有"回文串"的个数。这个回文串的意思是:所有子串(包裹不相邻的)只要满足回文串的性质都属于回文串.
    比如 acba 里面总共有 5个回文串 a,c,b,a,aa
     
    题解:区间DP,对于 i - j 区间,它满足条件的子串数量为 dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1] (子问题之和-重复子问题)
    如果str[i] ==str[j] 的话 ,那么在此基础上 满足条件的回文串数量还要加上 dp[i+1][j-1]+1 .
    因为 对于 i<t<=k<j  如果 str[t]和str[k] 组成回文串 那么  '字符M'+str[t] 和 str[k]+'字符M'又是一个新的回文串,加上本身自己又是个回文串,所以要
    加上 dp[i+1][j-1]+1
     
    #include<stdio.h>
    #include<algorithm>
    #include<string.h>
    #include<iostream>
    #define N 1005
    using namespace std;
    
    int dp[1005][1005];
    char str[1005];
    int main()
    {
        int tcase;
        int k = 1;
        scanf("%d",&tcase);
        while(tcase--){
            scanf("%s",str+1);
            int len = strlen(str+1);
            for(int i=1;i<=len;i++) dp[i][i]=1;
            for(int l=2;l<=len;l++){
                for(int i=1;i<=len-l+1;i++){
                    int j=i+l-1;
                    dp[i][j] = (dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+10007)%10007;///子问题推出父问题(减掉重复子问题)
                                                                                ///有减号要加mod防止出现负数
                    if(str[i]==str[j]){
                        dp[i][j] = (dp[i][j]+dp[i+1][j-1]+1)%10007;///如果str[i]str[j]相等,那么会多出dp[i+1][j-1]+1个回文串
                    }
                }
            }
            printf("Case %d: %d
    ",k++,dp[1][len]);
        }
        return 0;
    }
  • 相关阅读:
    Python学习第61天(html之form标签)
    Python学习第60天(html之body标签)
    Python学习第59天(web前端html /1))
    Python学习第58天(selector版本的ftp习题实现)
    Python学习第57天(异步IO)
    Python学习第56天(configpraser模块复习)
    Python学习第55天(IO多路复用)
    Python学习第54天(阻塞(blocking) IO和非阻塞(non-blocking)IO)
    如何通过Git Bash的命令行将电脑本地项目上传到自己的GitHub上
    第10周周博客
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5391988.html
Copyright © 2011-2022 走看看