zoukankan      html  css  js  c++  java
  • hdu4632

    Palindrome subsequence

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

    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

    一个DP,比赛时DP的状态搞错了.....

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <algorithm>
     4 #include <string.h>
     5 #include <math.h>
     6 #include <vector>
     7 #include <stack>
     8 using namespace std;
     9 #define ll long long int
    10 char a[1005];
    11 ll b[1005][1005];
    12 int main()
    13 {
    14     int n,i,m,j,t;
    15     cin>>n;
    16     for(i=0;i<n;i++)
    17     {
    18         scanf("%s",a);
    19         memset(b,0,sizeof(b));
    20         m=strlen(a);
    21         for(j=0;j<m;j++)
    22          b[j][j]=1;
    23          for(j=1;j<m;j++)
    24          for(t=j-1;t>=0;t--)
    25          {
    26              if(a[t]!=a[j])
    27               b[t][j]=(b[t+1][j]+b[t][j-1]-b[t+1][j-1]+10007)%10007;
    28               else 
    29               b[t][j]=(b[t+1][j]+b[t][j-1]+1)%10007;
    30          }
    31          printf("Case %d: %d
    ",i+1,b[0][m-1]);
    32     }
    33 }
    View Code
  • 相关阅读:
    matrix_last_acm_4
    matrix_last_acm_3
    matrix_last_acm_2
    matrix_last_acm_1
    2015亚洲区北京站网络赛
    poj 1062 昂贵的聘礼 最短路
    2-SAT !!
    hdu 4925
    hdu 4927 Java大数
    poj3687 拓扑排序 还没怎么搞明白 回头再想想
  • 原文地址:https://www.cnblogs.com/ERKE/p/3256954.html
Copyright © 2011-2022 走看看