码迷,mamicode.com
首页 > 其他好文 > 详细

nyoj 760

时间:2014-11-15 18:50:16      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   os   sp   for   div   on   

ee LCS again

时间限制:1000 ms  |  内存限制:65535 KB
难度:3
 
描述

There are A, B two sequences, the number of elements in the sequence is n、m;

Each element in the sequence are different and less than 100000.

Calculate the length of the longest common subsequence of A and B.

 
输入
The input has multicases.Each test case consists of three lines;
The first line consist two integers n, m (1 < = n, m < = 100000);
The second line with n integers, expressed sequence A;
The third line with m integers, expressed sequence B;
输出
For each set of test cases, output the length of the longest common subsequence of A and B, in a single line.
样例输入
5 4
1 2 6 5 4
1 3 5 4
样例输出
3
上传者
TC_胡仁东

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int dp[100005],g[100005];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp,0,sizeof(dp));
        int x;
        for(int i = 1; i <= n ; ++ i)
            scanf("%d",&x),dp[x]=i;
        int r = 0 ;
        for(int i = 1 ; i <= m ; ++ i)
        {
            scanf("%d",&x);
            if(dp[x])
            g[r++]=dp[x];
        }
        int p = 0 ;
        dp[p++] = g[0];
        for(int i = 1 ; i < r ; ++ i)
        if(dp[p-1] < g[i])
            dp[p++] = g[i];
        else
        {
            x  = lower_bound(dp,dp+p,g[i])-dp;
            dp[x] = g[i];
        }
        printf("%d\n",p);
    }
    return 0;
}

  

nyoj 760

标签:blog   http   io   ar   os   sp   for   div   on   

原文地址:http://www.cnblogs.com/a972290869/p/4099933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!