Description
熊大妈的奶牛在小沐沐的熏陶下开始研究信息题目。小沐沐先让奶牛研究了最长上升子序列,再让他们研究了最长公共子序列,现在又让他们要研究最长公共上升子序列了。
小沐沐说,对于两个串A,B,如果它们都包含一段位置不一定连续的数字,且数字是严格递增的,那么称这一段数字是两个串的公共上升子串,而所有的公共上升子串中最长的就是最长公共上升子串了。
奶牛半懂不懂,小沐沐要你来告诉奶牛什么是最长公共上升子串。不过,只要告诉奶牛它的长度就可以了。
Input Format
第一行N,表示A,B的长度。
第二行,串A。
第三行,串B。
Output Format
输出长度。
Sample Input
4
2 2 1 3
2 1 2 3
Sample Output
2
Hint
1<=N<=3000,A,B中的数字不超过maxlongint
Limitation
各个测试点1s
就是把LIS和LCS结合起来
fi,j
f
i
,
j
表示匹配到了
Ai,Bj
A
i
,
B
j
的方案数
若
Ai≠Bj
A
i
≠
B
j
则
fi,j=fi−1,j
f
i
,
j
=
f
i
−
1
,
j
若
Ai=Bj
A
i
=
B
j
则
fi,j=max1<=k<j,bk<Ai
f
i
,
j
=
m
a
x
1
<=
k
<
j
,
b
k
<
A
i
{
fi−1,k
f
i
−
1
,
k
}
枚举
j
j
的时候不变,维护一个决策最大值即可。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,k) for(int i = j;i <= k;++i)
#define repp(i,j,k) for(int i = j;i >= k;--i)
#define rept(i,x) for(int i = linkk[x];i;i = e[i].n)
#define P pair<int,int>
#define Pil pair<int,ll>
#define Pli pair<ll,int>
#define Pll pair<ll,ll>
#define pb push_back
#define pc putchar
#define mp make_pair
#define file(k) memset(k,0,sizeof(k))
#define ll long long
namespace fastIO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror = 0;
inline char nc(){
static char buf[BUF_SIZE],*p1 = buf+BUF_SIZE, *pend = buf+BUF_SIZE;
if(p1 == pend){
p1 = buf; pend = buf+fread(buf, 1, BUF_SIZE, stdin);
if(pend == p1){ IOerror = 1; return -1;}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror)return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
inline void read(ll &x){
bool sign = 0; char ch = nc(); x = 0;
for(; blank(ch); ch = nc());
if(IOerror) return;
if(ch == '-') sign = 1, ch = nc();
for(; ch >= '0' && ch <= '9'; ch = nc()) x = x*10+ch-'0';
if(sign) x = -x;
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace fastIO;
int n;
int a[3010],b[3010];
int f[3010][3010];
int main()
{
read(n);
rep(i,1,n) read(a[i]);rep(i,1,n) read(b[i]);
int ans = 0;
rep(i,1,n)
{
int val = 0;
rep(j,1,n)
{
if(a[i] == b[j])
f[i][j] = val + 1;
else f[i][j] = f[i-1][j];
if(b[j] < a[i]) val = max(val,f[i-1][j]);
ans = max(ans,f[i][j]);
}
}
printf("%d\n",ans);
return 0;
}