zoukankan      html  css  js  c++  java
  • Uva 10635

    题目

    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
    ", ti, leftlen);
        }
    
        return 0;
    }
    View Code
  • 相关阅读:
    使用UGUI实现拖拽功能(拼图小游戏)
    Lua入门基础
    (改)编程实验一 词法分析程序
    编程实验一 词法分析程序
    0909编译原理
    1014 C语言文法定义与C程序的推导过程
    词法分析总结
    0916作业二词法分析
    0909有关编译原理的解释
    语音文法
  • 原文地址:https://www.cnblogs.com/xuesu/p/10423979.html
Copyright © 2011-2022 走看看