zoukankan      html  css  js  c++  java
  • 22-Two(公共子序列的个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5791

                Two

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


    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
     
    Recommend
    wange2014   |   We have carefully selected several similar problems for you:  6263 6262 6261 6260 6259 
     

    题目分析:

    HDU多校第五场的1011。 
    题目大意是有两个序列,其长度为n和m。求他们的子序列相同的个数。 
    DP题,推导公式,dp[i][j]表示在第一个序列前i个位置和第二个序列前j个位置下,有多少种子序列相同情况。 
    可以推出,如果某个第一个序列的i位置与第二个序列j位置元素不同,其dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]。 
    将i和j分开看,如果没有i,第一个序列前i-1个与第二个序列前j个有多少种相同的,和第一个序列前i个与第二个序列前j-1个有多少相同。同时,需要减掉dp[i-1][j-1],因为在之前将第一个序列前i-1和第二个序列前j-1计算了两边。 
    如果第一个序列i位置与第二个序列j位置元素相同,需要加上i与j相同的一个之外,还需要加上dp[i-1][j-1],因为dp[i-1][j-1]可以与i配对相同,也可以与j配对相同,于是就需要重复计算一次。 
    dp[i][j]=dp[i-1][j]+dp[i][j-1]+1。(实际上对于最后相等的话,算上最后一对就有a[i - 1][j - 1] + 1种(肯定比a[i-1][j - 1多一种),不算的话,则是a[i-1][j] + a[i][j - 1] - a[i - 1][j - 1])

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    int a[1005];
    int b[1005]; 
    long long dp[1005][1005]; 
    int mod = 1000000007; 
     
    long long find(int i, int j){
    	if(i < 0 || j < 0)
    		return 0; 
    	if(dp[i][j] != -1){   //如果是 != 0 会TLE!!! 因为这个题就算搜索过也有大量为0的情况,故初始化的初始值要避免用0 
    		return dp[i][j];
    	}
    	if(a[i] == b[j]){
    		return dp[i][j] = (find(i - 1, j) + find(i, j - 1) + 1) % mod;  //这里实际上是还有 + find(i-1,j-1) - find(i-1,j-1); 
    	}
    	else{
    		return dp[i][j] = (find(i - 1, j) + find(i, j - 1) - find(i - 1, j - 1) + mod) % mod; //加一个mod防止有负的 
    	}
    }
     
    int main(){
    //	std::ios::sync_with_stdio(false);
    	int n, m;
    	while(cin >> n >> m){
    		memset(dp, -1, sizeof(dp));
    		for(int i = 0; i < n; i++){
    			cin >> a[i];
    //			scanf("%d", &a[i]);
    		} 
    		for(int j = 0; j < m; j++)
    			cin >> b[j];
    //			scanf("%d", &b[j]);
    		cout << find(n - 1, m - 1) << endl;		
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    外刊IT评论网
    9 More Mind-Blowing WebGL Demos
    主流开源许可协议比较(BSD,Apache,GPL,LGPL,MIT...)
    jsPlumb
    SharePoint Designer 2013 Workflow
    The Zip, GZip, BZip2 and Tar Implementation For .NET
    Config Sharepoint 2013 Workflow PowerShell Cmdlet
    iTextSharp
    模糊查询
    asp.net Cookie
  • 原文地址:https://www.cnblogs.com/zhumengdexiaobai/p/8612620.html
Copyright © 2011-2022 走看看