题目链接: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有两个序列a1,a2,...,an和b1,b2,...,bm. 他想找到它们的最长公共递增子序列, 并且这个子序列的值是连续的(x,x+1,...,y−1,y).
输入描述
输入包含多组数据, 第一行包含一个整数T表示测试数据组数. 对于每组数据: 第一行包含两个整数n和m (1≤n,m≤100000)表示两个序列的长度. 第二行包含n个整数: a1,a2,...,an (1≤ai≤106). 第三行包含m个整数: b1,b2,...,bm (1≤bi≤106). 输入最多有1000组数据, 并且所有数据中n与m的和不超过2×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;
}