#include<bits/stdc++.h>
#define MAXC 5000
using namespace std;
typedef long long ll;
ll A[MAXC],B[MAXC];
ll lenA,lenB;
ll dp[MAXC][MAXC];
bool visited[MAXC][MAXC];
ll calcLCS(ll i,ll j)
{
if(A[i]==0||B[j]==0)
return 0;
if(visited[i][j])
return dp[i][j];
ll ans=0;
if(A[i]==B[j])
ans=1+calcLCS(i+1,j+1);
else
{
ll val1=calcLCS(i+1,j);
ll val2=calcLCS(i,j+1);
ans=max(val1,val2);
}
visited[i][j]=1;
dp[i][j]=ans;
return dp[i][j];
}
int main()
{
ll i,j,k,l,m,n,test;
cin>>test;
for(k=1;k<=test;k++)
{
ll lenA=0;
while(1)
{
cin>>A[lenA];
lenA++;
if(A[lenA-1]==0)
{
break;
}
}
ll mx=-999999999;
lenB=0;
while(1)
{
cin>>B[lenB];
if(lenB==0&&B[lenB]==0)
{
break;
}
else
{
lenB++;
if(B[lenB-1]==0)
{
memset(visited,0,sizeof(visited));
mx=max(mx,calcLCS(0,0));
lenB=0;
}
}
}
cout<<mx<<endl;
}
return 0;
}
#define MAXC 5000
using namespace std;
typedef long long ll;
ll A[MAXC],B[MAXC];
ll lenA,lenB;
ll dp[MAXC][MAXC];
bool visited[MAXC][MAXC];
ll calcLCS(ll i,ll j)
{
if(A[i]==0||B[j]==0)
return 0;
if(visited[i][j])
return dp[i][j];
ll ans=0;
if(A[i]==B[j])
ans=1+calcLCS(i+1,j+1);
else
{
ll val1=calcLCS(i+1,j);
ll val2=calcLCS(i,j+1);
ans=max(val1,val2);
}
visited[i][j]=1;
dp[i][j]=ans;
return dp[i][j];
}
int main()
{
ll i,j,k,l,m,n,test;
cin>>test;
for(k=1;k<=test;k++)
{
ll lenA=0;
while(1)
{
cin>>A[lenA];
lenA++;
if(A[lenA-1]==0)
{
break;
}
}
ll mx=-999999999;
lenB=0;
while(1)
{
cin>>B[lenB];
if(lenB==0&&B[lenB]==0)
{
break;
}
else
{
lenB++;
if(B[lenB-1]==0)
{
memset(visited,0,sizeof(visited));
mx=max(mx,calcLCS(0,0));
lenB=0;
}
}
}
cout<<mx<<endl;
}
return 0;
}
No comments:
Post a Comment