This is program is for finding longest common subsequence for two given strings.
#include "stdafx.h"
#include "string.h"
void LCS();
void printLCS(int m, int n);
int a[100][100];
int b[100][100];
char seq1[100],seq2[100];
int _tmain(int argc, _TCHAR* argv[])
{
char Input;
printf("Please enter one sequence\n");
scanf("%s",&seq1);
printf("Please enter second sequence\n");
scanf("%s",&seq2);
LCS();
printLCS(strlen(seq1),strlen(seq2));
printf("\n");
fflush(stdout);
fflush(stdin);
printf("Enter inpt\n");
scanf("%c",&Input);
return 0;
}
void LCS()
{
int m = strlen(seq1);
int n = strlen(seq2);
for(int i=1;i<=m;i++)
for(int j=1;j<=n;j++)
{
if(seq1[i-1] == seq2[j-1])
{
a[i][j] = a[i-1][j-1] + 1;
b[i][j] = 0;
}
else
{
if(a[i][j-1] > a[i-1][j])
{
a[i][j] = a[i][j-1];
b[i][j] = 1; // horizontal
}
else
{
a[i][j] = a[i-1][j];
b[i][j] = 2; // vertical
}
}
}
}
void printLCS(int m,int n)
{
switch(b[m][n])
{
case 0:
printLCS(m-1,n-1);
printf("%c",seq1[m-1]);
break;
case 1:
printLCS(m,n-1);
break;
case 2:
printLCS(m-1,n);
break;
}
}
No comments:
Post a Comment