Advertisement
Rezaur_Rahman

LCS

Aug 17th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. char st1[100],st2[100];
  5. int len1,len2;
  6.  
  7. int LCS();
  8. int MAX(int a,int b);
  9.  
  10. int main()
  11. {
  12.     printf("Enter first sequence: ");
  13.     scanf("%s",&st1);
  14.     printf("Enter second sequence: ");
  15.     scanf("%s",&st2);
  16.  
  17.     len1=strlen(st1);
  18.     len2=strlen(st2);
  19.  
  20.     int ans=0;
  21.     ans=LCS();
  22.     printf("LCS: %d\n",ans);
  23.  
  24.     return 0;
  25. }
  26.  
  27. int LCS()
  28. {
  29.     int L[len1+1][len2+1];
  30.     int i,j;
  31.  
  32.     for(i=0;i<=len1;i++)
  33.         for(j=0;j<=len2;j++)
  34.         {
  35.             if(i==0 || j==0)
  36.                 L[i][j]=0;
  37.             else if(st1[i-1]==st2[j-1])
  38.                 L[i][j]=L[i-1][j-1]+1;
  39.             else
  40.                 L[i][j]=MAX(L[i-1][j],L[i][j-1]);
  41.         }
  42.  
  43.         return L[len1][len2];
  44. }
  45.  
  46. int MAX(int a,int b)
  47. {
  48.     if(a>b) return a;
  49.     else return b;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement