LCIS
This problem differs from one which was on the online contest.
The sequence a1, a2, …, an is called increasing, if ai < ai + 1 for i < n.
The sequence s1, s2, …, sk is called the subsequence of the sequence a1, a2, …, an, if there exist such a set of indexes 1 ≤ i1 < i2 < … < ik ≤ n that aij = sj. In other words, the sequence s can be derived from the sequence a by crossing out some elements.
You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
Input
The first line contains an integer n (1 ≤ n ≤ 500) — the length of the first sequence. The second line contains n space-separated integers from the range [0, 109] — elements of the first sequence. The third line contains an integer m (1 ≤ m ≤ 500) — the length of the second sequence. The fourth line contains m space-separated integers from the range [0, 109] — elements of the second sequence.
Output
In the first line output k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
Examples
input
7
2 3 1 6 5 4 6
4
1 3 5 6
output
3
3 5 6
input
5
1 2 0 2 1
3
1 0 1
output
2
0 1
题意: 求两个序列的最长公共上升子序列长度,及其路径的输出。
思路:
- d p [ i ] [ j ] dp[i][j] dp[i][j]表示在 a a a 序列中以位置 i i i 结尾,在 b b b 序列中以 j j j 位置结尾,并且以 b [ j ] b[j] b[j] 结尾的 L C I S LCIS LCIS长度,
- i f ( a [ i ] ! = b [ j ] ) d p [ i ] [ j ] = d p [ i − 1 ] [ j ] if(a[i] != b[j]) dp[i][j] = dp[i - 1][j] if(a[i]!=b[j]) dp[i][j]=dp[i−1][j]
- e l s e d p [ i ] [ j ] = m a x ( d p [ i − 1 ] [ k ] ) + 1 else dp[i][j] = max(dp[i - 1][k]) + 1 else dp[i][j]=max(dp[i−1][k])+1 其中 1 < = k < j 1 <= k < j 1<=k<j
有很简单的
O
(
N
3
)
O(N^3)
O(N3) 的写法。当然可以优化至
O
(
N
2
)
O(N^2)
O(N2)
路径输出利用
p
a
t
h
path
path数组可以很简单的还原路径
Code:
#include<bits/stdc++.h>
#define debug(x) cout << "[" << #x <<": " << (x) <<"]"<< endl
#define pii pair<int,int>
#define clr(a,b) memset((a),b,sizeof(a))
#define rep(i,a,b) for(int i = a;i < b;i ++)
#define pb push_back
#define MP make_pair
#define LL long long
#define ull unsigned LL
#define ls i << 1
#define rs (i << 1) + 1
#define fi first
#define se second
#define ptch putchar
#define CLR(a) while(!(a).empty()) a.pop()
using namespace std;
inline LL read() {
LL s = 0,w = 1;
char ch = getchar();
while(!isdigit(ch)) {
if(ch == '-') w = -1;
ch = getchar();
}
while(isdigit(ch))
s = s * 10 + ch - '0',ch = getchar();
return s * w;
}
inline void write(LL x) {
if(x < 0)
putchar('-'), x = -x;
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
const int maxn = 510;
int a[maxn],b[maxn],n,m;
int dp[maxn][maxn];
int pathy[maxn][maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
while(~scanf("%d",&n)){
clr(dp,0); clr(pathy,0);
rep(i,1,n + 1) a[i] = read();
m = read();
rep(i,1,m + 1) b[i] = read();
// rep(i,1,n + 1) rep(j,1,m + 1) dp[i][j] = 1;
int maxx = 0,idx,idy;
for(int i = 1;i <= n;++ i){
int tmp = 0,iid = 0;
for(int j = 1;j <= m;++ j){
if(tmp < dp[i - 1][j - 1] && a[i] > b[j - 1]){ //在n^3中,else就是a[i] == b[j]
tmp = dp[i - 1][j - 1];
iid = j - 1;
}
if(a[i] != b[j]){
dp[i][j] = dp[i - 1][j];
pathy[i][j] = j;
}
else {
dp[i][j] = tmp + 1;
pathy[i][j] = iid;
// for(int k = 1;k < j;++ k)
// if(b[k] < b[j] && dp[i][j] < dp[i - 1][k] + 1){
// pathy[i][j] = k;
// dp[i][j] = dp[i - 1][k] + 1;
// }
}
if(maxx < dp[i][j]) maxx = dp[i][j], idx = i, idy = j;
}
}
write(maxx); ptch('\n');
stack<int> st; CLR(st);
while(maxx){
if(st.empty() || b[idy] < st.top()){
st.push(b[idy]);
-- maxx;
}
idy = pathy[idx][idy];
-- idx;
}
while(!st.empty()) write(st.top()), st.pop(), ptch(' ');
ptch('\n');
}
return 0;
}