Description
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.
Sample Input
7 2 3 1 6 5 4 6 4 1 3 5 6
3 3 5 6
5 1 2 0 2 1 3 1 0 1
2
题意:给两个数列,求他们的公共最长子序列。
分析:
解法一:自己YY的方法,设dp[i][j]表示两个数列分别以i,j结尾且 ai == bi 时 的最长公共子序列长度,然后把所有状态按ai的顺序排序,用二维树状数组处理转移。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define MAXN 2100000007
using namespace std;
struct NEW
{
int val,x,y;
}dp[504][504],f[504][504],New[250005];
int n,m,tot,a[505],b[505],c[505];
stack <NEW> s;
int lowbit(int x)
{
return x & (-x);
}
void Insert(NEW k)
{
int x = k.x,y = k.y,val = k.val;
for(int a = x;a <= n;a += lowbit(a))
for(int b = y;b <= m;b += lowbit(b))
if(f[a][b].val < val) f[a][b] = k;
}
NEW Find(int x,int y)
{
NEW temp = {0,0,0};
for(int a = x;a;a -= lowbit(a))
for(int b = y;b;b -= lowbit(b))
if(temp.val < f[a][b].val) temp = f[a][b];
return temp;
}
bool camp(NEW a,NEW b)
{
return a.val < b.val;
}
int main()
{
cin.sync_with_stdio(false);
cin>>n;
for(int i = 1;i <= n;i++) cin>>a[i];
cin>>m;
for(int i = 1;i <= m;i++) cin>>b[i];
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(a[i] == b[j]) New[++tot] = {a[i],i,j};
sort(New+1,New+1+tot,camp);
for(int i = 1;i <= tot;i++)
{
if(New[i].val != New[i-1].val)
{
while(!s.empty())
{
Insert(s.top());
s.pop();
}
}
int x = New[i].x,y = New[i].y;
dp[x][y] = Find(x-1,y-1);
NEW temp = {dp[x][y].val+1,x,y};
s.push(temp);
}
int Max = 0;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(a[i] == b[j] && dp[i][j].val+1 > Max) Max = dp[i][j].val+1;
cout<<Max<<endl;
stack <int> tem;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
if(dp[i][j].val+1 == Max && a[i] == b[j])
{
tem.push(a[i]);
NEW temp = dp[i][j];
while(temp.val)
{
tem.push(a[temp.x]);
temp = dp[temp.x][temp.y];
}
while(!tem.empty())
{
cout<<tem.top()<<" ";
tem.pop();
}
return 0;
}
}
解法二: 网上的主流做法,dp[i][j]表示当前处理到第一个数列的i位,第二个数列的j位,且以b[j]结尾的最长子序列长度,则当a[i] != b[j]时 dp[i][j] = dp[i-1][j],否则dp[i][j] = max(dp[i-1][k]) (k < j 且 bk < bj),因为性质ai == bj,所以k只和ai相关,可以在第二维循环时同时处理。
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#define INF 0x3f3f3f3f
#define eps 1e-9
#define MOD 1000000007
#define MAXN 2100000007
using namespace std;
int n,m,a[502],b[502],dp[502][502],cho[502][502];
int main()
{
cin.sync_with_stdio(false);
cin>>n;
for(int i = 1;i <= n;i++) cin>>a[i];
cin>>m;
for(int i = 1;i <= m;i++) cin>>b[i];
a[0] = b[0] = -1;
for(int i = 1;i <= n;i++)
{
int temp = 0;
for(int j = 1;j <= m;j++)
{
if(a[i] != b[j])
{
dp[i][j] = dp[i-1][j];
cho[i][j] = j;
}
else
{
dp[i][j] = dp[i-1][temp] + 1;
cho[i][j] = temp;
}
if(a[i] > b[j] && dp[i-1][j] > dp[i-1][temp]) temp = j;
}
}
int Max = 0;
for(int i = 1;i <= m;i++) Max = max(Max,dp[n][i]);
cout<<Max<<endl;
stack <int> s;
for(int i = 1;i <= m;i++)
if(Max == dp[n][i])
{
int temp = i;
if(a[n] == b[temp]) s.push(b[temp]);
temp = cho[n][temp];
for(int k = n-1;k;k--)
{
if(a[k] == b[temp]) s.push(b[temp]);
temp = cho[k][temp];
}
break;
}
while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
}