#include <iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>vc;
vector<int>vc2;
int lcs(int m,int n)
{
int ar[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0||j==0) ar[i][j]=0;
else if(vc[i-1]==vc2[j-1]) ar[i][j]=ar[i-1][j-1]+1;
else ar[i][j]=max(ar[i-1][j],ar[i][j-1]);
}
}
return ar[m][n];
}
int main()
{
int m,n,p,cas=0;
while(~scanf("%d%d",&m,&n))
{
if(m==0&&n==0) break;
for(int i=0;i<m;i++){cin>>p;vc.push_back(p);}
for(int i=0;i<n;i++){cin>>p;vc2.push_back(p);}
int res=lcs(m,n);
printf("Twin Towers #%d\nNumber of Tiles : %d\n",++cas,res);
vc.clear();vc2.clear();
printf("\n");
}
return 0;
}
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>vc;
vector<int>vc2;
int lcs(int m,int n)
{
int ar[m+1][n+1];
for(int i=0;i<=m;i++){
for(int j=0;j<=n;j++){
if(i==0||j==0) ar[i][j]=0;
else if(vc[i-1]==vc2[j-1]) ar[i][j]=ar[i-1][j-1]+1;
else ar[i][j]=max(ar[i-1][j],ar[i][j-1]);
}
}
return ar[m][n];
}
int main()
{
int m,n,p,cas=0;
while(~scanf("%d%d",&m,&n))
{
if(m==0&&n==0) break;
for(int i=0;i<m;i++){cin>>p;vc.push_back(p);}
for(int i=0;i<n;i++){cin>>p;vc2.push_back(p);}
int res=lcs(m,n);
printf("Twin Towers #%d\nNumber of Tiles : %d\n",++cas,res);
vc.clear();vc2.clear();
printf("\n");
}
return 0;
}
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন