Problem Type : DP (Algorithm-> Longest common Subsequence)
#include<stdio.h>
#include<string.h>
int arr[1000][1000];
int main()
{
int n,m,i,j,k,x[1000],y[1000],p,q,cas=0;
while(scanf("%d %d",&n,&m)==2)
{
if(n==0 && m==0)
break;
cas ++;
memset(arr,0,sizeof(arr));
for(i=1;i<=n;i++)
{
scanf("%d",&x[i]);
}
for(i=1;i<=m;i++)
{
scanf("%d",&y[i]);
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(y[i]==x[j])
{
arr[i][j]= arr[i-1][j-1]+1;
// printf("%d\n",arr[i][j]);
}
else
{
p=arr[i-1][j];
// printf("%d\n",p);
q = arr[i][j-1];
//printf("%d\n",q);
if(p>q)
{
arr[i][j]= p;
}
else
{
arr[i][j]=q;
}
}
}
}
printf("Twin Towers #%d\n",cas);
printf("Number of Tiles : %d\n\n",arr[m][n]);
}
}
No comments:
Post a Comment