zoukankan      html  css  js  c++  java
  • HDU 5791 Two

    Two

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 163    Accepted Submission(s): 72


    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
     状态方程 dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+(a[i]==b[j]?dp[i-1][j-1]+1:0)
    可以这么理解:
    比如序列
    a :1 2
    b :1 1 2
    当i=2  j=3
    首先不考虑a[2]和b[3]是什么  那么此时dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]把  i=2  j=3带入即可
    再考虑 a[2]==b[3]时  这时 多增加了 1+dp[i-1][j-1]对,即a[2]和b[3]匹配和 a[2]b[3]和前面dp[i-1][j-1]匹配的对数。
    a[2]!=b[3]时就不用考虑增加的....
     
    注意 子序列可以不连续...一开始卡在这里了...zz
    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/8/2 14:20:58
    File Name     :p511.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 1000000007
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    ll dp[1100][1100];
    int n,m;
    int a[1100];
    int b[1100];
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        while(cin>>n>>m){
            for(int i=1;i<=n;i++)scanf("%d",&a[i]);
            for(int j=1;j<=m;j++)scanf("%d",&b[j]);
            cle(dp);
            ll x;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(a[i]==b[j])x=dp[i-1][j-1]+1;
                    else x=0;
                    dp[i][j]=(dp[i][j]+dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+mod)%mod;
                    dp[i][j]=(dp[i][j]+x)%mod;
                }
            }
            printf("%I64d
    ",dp[n][m]);
        }
        return 0;
    }
  • 相关阅读:
    MongoDB结构划分
    iphone下scrollview图片浏览器小记
    图文详解linux/windows mysql忘记root密码解决方案
    【记】Javascript遍历对象属性的方法
    【jQuery】jQueryUI中的datepicker在overflow下的两点点小小的问题。
    第一个测试文章
    【记】Javascript的正则表达式RegExp
    【记】IE下input标签中的paddingleft和paddingright
    【CSS】关于IE、FireFox中table强制换行的总结
    【DOCTYPE】兼容模式和标准模式
  • 原文地址:https://www.cnblogs.com/pk28/p/5730375.html
Copyright © 2011-2022 走看看