zoukankan      html  css  js  c++  java
  • Palindrome subsequence(区间dp+容斥)

    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 = <S x1, S x2, ..., S xk> and Y = <Sy1, S y2, ..., S yk> , 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 xi = S yi. 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

    4
    a
    aaaaa
    goodafternooneveryone
    welcometoooxxourproblems

    Sample Output

    Case 1: 1
    Case 2: 31
    Case 3: 421
    Case 4: 960

    大致题意是给定一个字符串,求回文子序列个数,最后的答案要%10007

    首先定义f数组f[l][r]表示l~r区间的回文子序列个数,f[i][i]=1;

    显然 根据容斥原理 :f[l][r]=f[l][r-1]+f[l+1][r]-f[l+1][r-1]  (因为中间的个数会算两遍);

    然后,我们考虑s[l]==s[r]的情况,如果这两个位置相等,那么l+1 ~ r-1这个区间的所有子序列,都可以加入l和r这两个元素,构成一个新的回文子序列,除此之外 l和r这两个元素也可以构成一个回文子序列

    注意减的时候取模+要加模

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<vector>
    #include<map>
    #include<cmath>
    const int maxn=1e5+5;
    const int mod=10007;
    typedef long long ll;
    using namespace std;
    char str[1005];
    ll dp[1005][1005];
    int main()
    {
       int T;
       int cnt=1;
       cin>>T;
       for(int t=1;t<=T;t++)
       {
           scanf("%s",str+1);
           int len=strlen(str+1);
           memset(dp,0,sizeof(dp));
           for(int t=1;t<=len;t++)
           {
               dp[t][t]=1;
               if(t<len&&str[t]==str[t+1])
               {
                   dp[t][t+1]=3;
            }
            else
            {
                dp[t][t+1]=2;
            }
        }
        for(int l=2;l<=len;l++)
        {
            for(int j=1;j+l<=len;j++)
            {
                int r=j+l;
                if(str[j]==str[r])
                {
                    dp[j][r]=(dp[j+1][r]+dp[j][r-1]+1)%mod;
                }
                else
                dp[j][r]=(dp[j][r-1]+dp[j+1][r]-dp[j+1][r-1]+mod)%mod;
            }
        }    
        printf("Case %d: %lld
    ",cnt++,dp[1][len]);
        
       } 
       return 0;
    }
  • 相关阅读:
    总结下JavaWeb应用里正确显示中文需要的设置
    JDBC连接MySQL数据库的示例代码
    ZT:CSS实现水平|垂直居中漫谈
    今天整理了下所有博文
    关于后台数据库正常存储中文通过Ajax方式传递到前台变成问号的处理
    回顾以前的线程安全的类
    同步解决线程安全问题的三种实现
    如何判断一个程序是否会有线程安全问题?
    Java中如何通过一个类名来调用另一个类的静态方法?
    作为一个程序员,数学对你到底有多重要?!
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/11228801.html
Copyright © 2011-2022 走看看