Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- int main()
- {
- int i=0,j=0,lcs[100][100],lnx,lny,k;
- char x[100],y[100];
- printf("Enter the first(x) string : ");
- scanf("%s",&x);
- printf("Enter the second(y) string : ");
- scanf("%s",&y);
- lnx = strlen(x);
- lny = strlen(y);
- for(i=1; i<=lnx; i++)
- {
- lcs[i][0]=0;
- }
- for(j=1; j<=lny; j++)
- {
- lcs[0][j]=0;
- }
- for(i=1; i<=lnx; i++)
- {
- for(j=1; j<=lny; j++)
- {
- if(x[i-1]==y[j-1])
- {
- lcs[i][j]=lcs[i-1][j-1]+1;
- }
- else if(lcs[i-1][j]>=lcs[i][j-1])
- {
- lcs[i][j]=lcs[i-1][j];
- }
- else
- {
- lcs[i][j]=lcs[i][j-1];
- }
- }
- }
- printf("\n\nLCS table:\n\n ");
- for(k=0; k<lny; k++)
- {
- printf("%c ",y[k]);
- }
- printf("\n ");
- for(i=0; i<=lnx; i++)
- {
- if(i!=0)
- {
- printf("%c",x[i-1]);
- }
- for(j=0; j<=lny; j++)
- {
- printf(" %d",lcs[i][j]);
- }
- printf("\n");
- }
- printf("\n\nThe lenght of LCS is %d\n",lcs[lnx][lny]);
- i=lnx;
- j=lny;
- while(i>0 && j>0){
- if(lcs[i-1][j]!=lcs[i][j] && lcs[i][j-1]!=lcs[i][j]){
- printf("%c",x[i-1]);
- i--;
- j--;
- }
- else if(lcs[i-1][j]==lcs[i][j]){
- i--;
- }
- else if(lcs[i][j-1]==lcs[i][j]){
- j--;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement