zoukankan      html  css  js  c++  java
  • hdu 5791 Two dp

    Two

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


    Problem Description
    Alice gets two sequences A and B. A easy problem comes. How many pair of sequence A' and sequence B' are same. For example, {1,2} and {1,2} are same. {1,2,4} and {1,4,2} are not same. A' is a subsequence of A. B' is a subsequence of B. The subsequnce can be not continuous. For example, {1,1,2} has 7 subsequences {1},{1},{2},{1,1},{1,2},{1,2},{1,1,2}. The answer can be very large. Output the answer mod 1000000007.
     
    Input
    The input contains multiple test cases.

    For each test case, the first line cantains two integers N,M(1N,M1000). The next line contains N integers. The next line followed M integers. All integers are between 1 and 1000.
     
    Output
    For each test case, output the answer mod 1000000007.
     
    Sample Input
    3 2 1 2 3 2 1 3 2 1 2 3 1 2
     
    Sample Output
    2 3
     
    Author
    ZSTU
     
    Source

    2016 Multi-University Training Contest 5

    思路:if(a[i]==b[t])
             dp[i][t]=((1+dp[i][t-1]+dp[i-1][t])%mod+mod)%mod;
             else
             dp[i][t]=((dp[i][t-1]+dp[i-1][t]-dp[i-1][t-1])%mod+mod)%mod;

        dp[i][t]表示答案,A数组的i位置,b数组的t位置;

        利用前缀减少时间复杂度,从前面继承;

        ps:又忘了取模的坑点,减法需要+mod%mod;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define esp 0.00000000001
    const int N=1e3+10,M=1e7+10,inf=1e9+10,mod=1000000007;
    int a[N];
    int b[N];
    ll dp[N][N];
    int main()
    {
        int x,y,z,i,t;
        while(~scanf("%d%d",&x,&y))
        {
            memset(dp,0,sizeof(dp));
            for(i=1;i<=x;i++)
            scanf("%d",&a[i]);
            for(i=1;i<=y;i++)
            scanf("%d",&b[i]);
            for(i=1;i<=x;i++)
            {
                for(t=1;t<=y;t++)
                if(a[i]==b[t])
                dp[i][t]=((1+dp[i][t-1]+dp[i-1][t])%mod+mod)%mod;
                else
                dp[i][t]=((dp[i][t-1]+dp[i-1][t]-dp[i-1][t-1])%mod+mod)%mod;
            }
            printf("%I64d
    ",dp[x][y]);
        }
        return 0;
    }
  • 相关阅读:
    互联网协议入门
    【HTTP】图解HTTPS
    《计算机本科生理想的学习计划》
    VC++ TinyXML
    TinyXML 在vs2010 VC++使用
    Hadoop2.4.1入门实例:MaxTemperature
    xcode6
    Android利用广播监听设备网络连接(断网)的变化情况
    编程算法
    Google的Guava之IO升华
  • 原文地址:https://www.cnblogs.com/jhz033/p/5734362.html
Copyright © 2011-2022 走看看