hdu 5904 LCIS (Best Coder 1003 最长公共子序列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5904


LCIS

 
 Accepts: 109
 
 Submissions: 775
 Time Limit: 4000/2000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
Alex有两个序列a_1,a_2,...,a_na1,a2,...,anb_1,b_2,...,b_mb1,b2,...,bm. 他想找到它们的最长公共递增子序列, 并且这个子序列的值是连续的(x,x+1,...,y-1,yx,x+1,...,y1,y).
输入描述
输入包含多组数据, 第一行包含一个整数TT表示测试数据组数. 对于每组数据:

第一行包含两个整数nnmm (1 \le n, m \le 100000)(1n,m100000)表示两个序列的长度. 第二行包含nn个整数: a_1, a_2, ..., a_na1,a2,...,an (1 \le a_i \le 10^6)(1ai106). 第三行包含mm个整数: b_1, b_2, ..., b_mb1,b2,...,bm (1 \le b_i \le 10^6)(1bi106).

输入最多有10001000组数据, 并且所有数据中nnmm的和不超过2 \times 10^62×106.
输出描述
对于每组数据, 输出一个整数表示长度.
输入样例
3
3 3
1 2 3
3 2 1
10 5
1 23 2 32 4 3 4 5 6 1
1 2 3 4 5
1 1
2
1
输出样例
1
5
0

题目大意:略

解析:先求最长连续子序列,然后找出两串中相同 对应 最长连续子序列  最大值即可,

            借鉴大牛博客:http://www.cnblogs.com/bin-gege/p/5904338.html


代码如下:

#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
#include<queue>
#include<set>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 100009
using namespace std;
const int inf = 1e9;
const int mod = 1<<30;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef long long LL;

int m, n, a[N], b[N], aa[N], bb[N];
int solve()
{
    memset(aa, 0, sizeof(aa));
    memset(bb, 0, sizeof(bb));
    for(int i = 1; i <= m; i++)  aa[a[i]] = max(aa[a[i]], aa[a[i] - 1] + 1);
    for(int i = 1; i <= n; i++)  bb[b[i]] = max(bb[b[i]], bb[b[i] - 1] + 1);
    int ans = 0;
    for(int i = 1; i <= m; i++)  ans = max(ans, min(aa[a[i]], bb[a[i]])); //无所谓,哪串都行
    return ans;
}
int main()
{
    int t, i;
    cin >> t;
    while(t--)
    {
        scanf("%d%d", &m, &n);
        for(i = 1; i <= m; i++) scanf("%d", &a[i]);
        for(i = 1; i <= n; i++) scanf("%d", &b[i]);
        int ans = solve();
        printf("%d\n", ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值