/*111*/
#include<stdio.h>
#include<algorithm>
using namespace std;
int st[30];
int in[30];
int LCS(int len)
{
int dp1[30],dp2[30];
int *A,*B,*temp;
A=dp1;B=dp2;
if (st[0]==in[0])A[0]=1;
else A[0]=0;
for (int j=1;j<len;j++)
{
if (st[0]==in[j])
A[j]=1;
else
A[j]=0;
}
for (int i=1;i<len;i++)
{
if (st[i]==in[0])
B[0]=1;
else B[0]=A[0];
for (int j=1;j<len;j++)
{
if (st[i]==in[j])
{
B[j]=A[j-1]+1;
}
else B[j]=max(B[j-1],A[j]);
}
temp=A;A=B;B=A;
}
return A[len-1];
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int len;
int k;
scanf("%d",&len);
for (int i=0;i<len;i++)
{
scanf("%d",&k);
st[k-1]=i+1;
}
while (scanf("%d",&k)!=EOF)
{
in[k-1]=1;
for (int i=1;i<len;i++)
{
scanf("%d",&k);
in[k-1]=i+1;
}
printf("%d\n",LCS(len));
}
return 0;
}
Can someone plzzz explain the o/p of the following sample input....how is it coming to 9...according to me answer should be 5 for last case.
last case LIS - 2 4 9 5 7
Perhaps I missed it in another category or subcat, but my boss pointed out there is no way to monitor user activity through the admin interface. Has someone developed a plugin or mod that generates a report including time, userid, username, email address, and action (subscribe, unsubscribe, or modify)? He recently posted a home page article on our website to elicit subscriptions but we have no easy way to identify its effectiveness.
I see that this is a straight forward LCS problem. Also I get the fact that as the numbers are unique we can use LIS after mapping the first sequence to the sequence 1,2.....n.
Competitive programming book (1st edition) claims that this is a straight forward LIS implementation. Is that so? So most of the times LIS is used to speed up LCS.
int arr1[10000],arr2[10000];
int lcs( int *X, int *Y, int m, int n )
{
int L[m+1][n+1];
int i, j;
for (i=0; i<=m; i++)
{
for (j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}
int main()
{
int n,i,j,k;
cin>>n;
for (i=0; i<n; i++) {
cin>>arr1[i];
}
for (j=0;j<n-1;j++){
for (k=0; k<n; k++) {
cin>>arr2[k];
}
cout<<"\n"<<lcs(arr1,arr2,n,n);
}
return 0;
}
#include <iostream>
using namespace std;
int arr1[10000],arr2[10000];
int lcs( int *X, int *Y, int m, int n )
{
int L[m+1][n+1];
int i, j;
for (i=0; i<=m; i++)
{
for (j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X[i-1] == Y[j-1])
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
}
int main()
{
int n,i,j,k;
cin>>n;
//int arr1[n],arr2[n];
for (i=0; i<n; i++) {
cin>>arr1[i];
}
for (j=0;j<n-1;j++){
for (k=0; k<n; k++) {
cin>>arr2[k];
}
cout<<"\n"<<lcs(arr1,arr2,n,n);
}
return 0;
}