Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- int i,j,m,n,c[50][50],count=0;
- char x[50],y[50],b[50][50];
- void print_LCS(int i,int j)
- {
- if(i==0 || j==0)
- {
- return;
- }
- if(b[i][j]=='c')
- {
- print_LCS(i-1,j-1);
- printf("%c",x[i-1]);
- count++;
- }
- else if(b[i][j]=='u')
- {
- print_LCS(i-1,j);
- }
- else
- {
- print_LCS(i,j-1);
- }
- }
- void LCS_Length()
- {
- m=strlen(x);
- n=strlen(y);
- for(i=0; i<=m; i++)
- {
- c[i][0]=0;
- }
- for(i=0; i<=n; i++)
- {
- c[0][i]=0;
- }
- ///c -> corner
- ///l -> left
- ///u -> up
- 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]='c';
- }
- else if(c[i-1][j]>=c[i][j-1])
- {
- c[i][j]=c[i-1][j];
- b[i][j]='u';
- }
- else
- {
- c[i][j]=c[i][j-1];
- b[i][j]='l';
- }
- }
- }
- int main()
- {
- printf("Enter 1st sequence:");
- scanf("%s",x);
- printf("Enter 2nd sequence:");
- scanf("%s",y);
- printf("\nThe Longest Common Subsequence is ");
- LCS_Length();
- printf("<");
- print_LCS(m,n);
- printf("> \nThe Length of Subsequence is %d \n",count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement