现在的位置: 首页 > 综合 > 正文

NYOJ 760 See LCS again

2019年02月25日 ⁄ 综合 ⁄ 共 809字 ⁄ 字号 评论关闭

题目链接~~>

做题感悟:这题主要考想法,其实是让求最长单调递增子序列。

解题思路:首先读题要仔细题目说每个数组里的数两两不同,说明下标有用,数不超过100000,且数组范围100000,说明得开数组且时间为1000MS说明只要一次遍历即可。好,步入正题:先把第一个数组的值所对应的下标记录下来,第二个数组的值在第一个数组中找(O(1)的复杂度),依次记录下表,然后求最长单调递增子序列就可以了。

代码:

#include<stdio.h>
#include<iostream>
#include<map>
#include<stack>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std ;
const double PI = 3.1415926 ;
const double INF = 99999999 ;
const int MX =100005 ;
int dp[MX],g[MX] ;
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 ;
}

 

抱歉!评论已关闭.