zoukankan      html  css  js  c++  java
  • uva 111 History Grading(最长公共子序列)

    题目连接:111 - History Grading


    题目大意:给出一个n 代表序列中元素的个数, 然后是一个答案, 接下来是若干个同学的答案(直到文件结束为止), 求出两个序列的最长公共子序列, 注意给出的答案均是以该事件处于第几个发生的, 例如 :2 3 4 1

    即是 对应第1个事件在第2个发生,第2个事件在第3个发生 ...转换一下就是  4 1 2 3。


    解题思路:最长公共子序列问题, 状态转移方程

    d[i][j] = 

    0( i == 0 ||  j == 0)

    d[i - 1] [j - 1] + 1 ( a[i] == b[i])

    max(d[i - 1][j] , d[i] [j - 1])


    #include <stdio.h>
    #include <string.h>
    const int N = 1005;
    int max (const int &x, const int& y) { return x > y ? x : y; }
    
    int a[N], b[N], dp[N][N];
    int main() {
        int n, t;
        while (scanf("%d", &n) == 1) {
    	for (int i = 1; i <= n; i++) {
    	    scanf("%d", &t);
    	    a[t] = i;
    	}
    
    	while (scanf("%d", &t) == 1) {
    	    b[t] = 1;
    
    	    for (int i = 2; i <= n; i++) {
    		scanf("%d", &t);
    		b[t] = i;
    	    }
    
    	    memset(dp, 0, sizeof(dp));
    	    for (int i = 1; i <= n; i++)
    		for (int j = 1; j <= n; j++) {
    		    if (a[i] != b[j])
    			dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    		    else
    			dp[i][j] = dp[i - 1][j - 1] + 1;
    		}
    
    	    printf("%d
    ", dp[n][n]);
    	}
        }
        return 0;
    }
    
  • 相关阅读:
    Android测试入门篇
    SQL的基本知识
    正则表达式
    ES5语法
    vscode
    继承小结
    工作遇到的问题
    后台程序员的HTTP缓存
    xhr下载图片/服务器向客户端推送消息
    HTTP2.0
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3301785.html
Copyright © 2011-2022 走看看