zoukankan      html  css  js  c++  java
  • HDU 4632 Palindrome subsequence

    Palindrome subsequence

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/Others)

    Total Submission(s): 613    Accepted Submission(s): 232

    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
     
    Source
     

     给一个字符串,挑出里面的回文子串(子串中的子符在原串中可以了连续。

    DP问题,可以推出如下状态转移方程:

      若s[i]==s[j],dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+dp[i+1][j-1]+1

                  =dp[i+1][j]+dp[i][j-1]+1;

      反之若s[i]!=s[j],dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];

      初始条件为dp[i][i]=1;

           s[i]==s[i+1]时,dp[i][i+1]=3;

           s[i]!=s[i+1]时,dp[i][i+1]=2;

    多校第四场的第一题,比赛时不是我写的,据他们说写成递归的DP会超时……题出的没人性……

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 int len;
     8 int dp[1050][1050];
     9 char s[2000];
    10 
    11 
    12 int main()
    13 {
    14     int t;
    15 
    16     scanf("%d",&t);
    17     getchar();
    18 
    19     for(int k=1;k<=t;k++)
    20     {
    21         gets(s);
    22 
    23         len=strlen(s);
    24 
    25         for(int i=0;i<len;i++)
    26             dp[i][i]=1;
    27         for(int i=0;i<len-1;i++)
    28             if(s[i]==s[i+1])
    29                 dp[i][i+1]=3;
    30             else
    31                 dp[i][i+1]=2;
    32 
    33         for(int i=0;i<len;i++)
    34             for(int j=i-2;j>=0;j--)
    35                 if(s[i]==s[j])
    36                     dp[j][i]=(dp[j+1][i]+dp[j][i-1]+10008)%10007;
    37                 else
    38                     dp[j][i]=(dp[j+1][i]+dp[j][i-1]-dp[j+1][i-1]+10007)%10007;
    39 
    40         printf("Case %d: %d
    ",k,dp[0][len-1]);
    41     }
    42 
    43     return 0;
    44 }
    [C++]
  • 相关阅读:
    关于词向量工作原理的理解
    LDA-线性判别分析(四)其他几个相关问题
    LDA-线性判别分析(三)推广到 Multi-classes 情形
    LDA-线性判别分析(一)预备知识
    LDA-线性判别分析(二)Two-classes 情形的数学推导
    为什么国内的网盘公司都在 TB 的级别上竞争,成本会不会太高?
    为什么我们喜欢用 sigmoid 这类 S 型非线性变换?
    UFLDL 教程学习笔记(四)主成分分析
    关于协方差矩阵的理解
    UFLDL 教程学习笔记(三)自编码与稀疏性
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3231741.html
Copyright © 2011-2022 走看看