[text] q2

Viewer

  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int max(int a, int b) {
  5.     return (a > b) ? a : b;
  6. }
  7.  
  8. int lcs(char *str1, char *str2, int len1, int len2) {
  9.     int lcs[len1+1][len2+1];
  10.     int i, j;
  11.  
  12.     for (i = 0; i <= len1; i++) {
  13.         for (j = 0; j <= len2; j++) {
  14.             if (i == 0 || j == 0) {
  15.                 lcs[i][j] = 0;
  16.             } else if (str1[i-1] == str2[j-1]) {
  17.                 lcs[i][j] = lcs[i-1][j-1] + 1;
  18.             } else {
  19.                 lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1]);
  20.             }
  21.         }
  22.     }
  23.     return lcs[len1][len2];
  24. }
  25.  
  26. int main() {
  27.     char str1[] = "VITCHENNAI";
  28.     char str2[] = "VITVELLORE";
  29.  
  30.     int len1 = strlen(str1);
  31.     int len2 = strlen(str2);
  32.  
  33.     int result = lcs(str1, str2, len1, len2);
  34.  
  35.     printf("%d\n", result);
  36.  
  37.     return 0;
  38. }
  39.  

Editor

You can edit this paste and save as new:


File Description
  • q2
  • Paste Code
  • 24 Mar-2023
  • 834 Bytes
You can Share it: