Advertisement
Kawsar_Hossain

LCS

Aug 9th, 2020
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     char s1[50],s2[50];
  6.     int i,j,len1,len2;
  7.     printf("Enter the first string: ");
  8.     gets(s1);
  9.     printf("Enter the second string: ");
  10.     gets(s2);
  11.     len1=strlen(s1);
  12.     len2=strlen(s2);
  13.     int len[len1+1][len2+1];
  14.     for(i=0;i<=len1;i++)
  15.     {
  16.         len[i][0]=0;
  17.     }
  18.     for(i=0;i<=len2;i++)
  19.     {
  20.         len[0][i]=0;
  21.     }
  22.     for(i=1;i<=len1;i++)
  23.     {
  24.         for(j=1;j<=len2;j++)
  25.         {
  26.             if(s1[i-1]==s2[j-1])
  27.             {
  28.                 len[i][j]=len[i-1][j-1]+1;
  29.             }
  30.             else
  31.             {
  32.                 if(len[i-1][j]>len[i][j-1])
  33.                 {
  34.                     len[i][j]=len[i-1][j];
  35.                 }
  36.                 else
  37.                 {
  38.                     len[i][j]=len[i][j-1];
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     printf("Length: %d", len[len1][len2]);
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement