LCS
Time Limit: 1000ms
Memory Limit: 128M
Description:
给两个长度为n的全排列,求其最长公共子序列长度。
Input:
第一行是一个正整数N,表示全排列长度。
第二行有n个整数,保证是一个n的全排列。
第三行有n个整数,保证是一个n的全排列。
Output:
输出第一行有一个整数,表示两数组最长公共子序列长度。
Sample Input:
5
1 3 2 4 5
5 2 3 1 4
Sample Output:
2
Hint:
对于80%的数据 n <= 1000
对于100%的数据 n <= 1000000
#include<iostream>
using namespace std;
int x[1000005],y[100005];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int c[3][n+1];
for(int i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&y[i]);
}
for(int i=1;i<=2;i++)c[i][0]=0;
for(int i=1;i<=n;i++)c[0][i]=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(x[i]==y[j])
{
c[2][j]=c[1][j-1]+1;
}
else if(c[2][j]>=c[2][j-1])
{
c[2][j]=c[1][j];
}
else
{
c[2][j]=c[2][j-1];
}
}
for(int j=1;j<=n;j++)
{
c[1][j]=c[2][j];
}
}
cout<<c[2][n]<<endl;
}
return 0;
}