#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 20
int c[max+1][max+1];
char b[max+1][max+1];
char x[max],y[max];
int m,n;
void showLCS(int i,int j)
{
if(i!=0 && j!=0)
{
if(b[i][j] == '\\')
{
showLCS(i-1,j-1);
printf("%c",x[i-1]);
}
else if(b[i][j] == '|')
showLCS(i-1,j);
else
showLCS(i,j-1);
}
}
int main()
{
int i,j;
printf("\n Enter 1st string : ");
gets(x);
printf("\n Enter 2nd string : ");
gets(y);
m = strlen(x);
n = strlen(y);
for(i=1;i<=m;i++)
{
c[i][0] = 0;
b[i][0] = ' ';
}
for(j=0;j<=n;j++)
{
c[0][j] = 0;
b[0][j] = ' ';
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(x[i-1] == y[j-1])
{
c[i][j] = c[i-1][j-1]+1;
b[i][j] = '\\';
}
else
{
if(c[i-1][j] >= c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = '|';
}
else
{
c[i][j] = c[i][j-1];
b[i][j] = '_';
}
}
}
}
for(i=0;i<=m;i++)
{
printf("\n");
for(j=0;j<=n;j++)
printf(" %c%d",b[i][j],c[i][j]);
}
getch();
printf("\n\n The LCS of both strings : ");
showLCS(m,n);
getch();
return 0;
}
No comments:
Post a Comment