首页 > 其他 > 详细

Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2

时间:2019-02-23 20:24:59      阅读:123      评论:0      收藏:0      [点我收藏+]

题目

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1576


题意

两个元素互不相同(在自身数列中互不相同)的数列,求二者的LCS。

 

思路

如刘书,

非常棒的思路:

1. 明显LCS问题只要关注两个数列的交集,利用第一个数列a的元素,对第二个数列b中的元素重编号(b元素中没有出现在a数列中的可以忽略)问题就转化为最长上升子序列问题。

2. 使用lower_bound不断维护数组leftNum,其中leftNum[i]是上升子序列延伸到i+1长度时最后一个元素(也是最大的那个元素,比如2,3,5中的5)的最小值

感想

1. 反射性地想用树状数组维护leftNum

 

代码

技术分享图片
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#define LOCAL_DEBUG
using namespace std;
typedef pair<int, int> MyPair;
const int MAXN = 250 * 250 + 1;
int b[MAXN];
int leftNum[MAXN];

int main() {
#ifdef LOCAL_DEBUG
    freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\input.txt", "r", stdin);
    //freopen("C:\\Users\\Iris\\source\\repos\\ACM\\ACM\\output.txt", "w", stdout);
#endif // LOCAL_DEBUG
    int T;
    scanf("%d", &T);
    for (int ti = 1; ti <= T; ti++) {
        int n, p, q;
        scanf("%d%d%d", &n, &p, &q);
        map<int, int> a2ind;
        for (int i = 0; i <= p; i++) {
            int tmp;
            scanf("%d", &tmp);
            a2ind[tmp] = i;
        }
        int blen = 0;
        for (int i = 0; i <= q; i++) {
            int tmp;
            scanf("%d", &tmp);
            if (a2ind.count(tmp) != 0) {
                b[blen++] = a2ind[tmp];
            }
        }
        int leftlen = 0;
        for (int i = 0; i < blen; i++) {
            int ind = lower_bound(leftNum, leftNum + leftlen, b[i]) - leftNum;
            leftNum[ind] = b[i];
            if (ind == leftlen)leftlen++;
        }
        printf("Case %d: %d\n", ti, leftlen);
    }

    return 0;
}
View Code

 

Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2

原文:https://www.cnblogs.com/xuesu/p/10423979.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!