Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <stdlib.h>
  6.  
  7. using namespace std;
  8.  
  9.  
  10. int getLCS(string &A, string &B) {
  11.     A=" "+A;
  12.     B=" "+B;
  13.     int l[A.size() + 1][B.size() + 1];
  14.     int a;
  15.     l[0][0]=0;
  16.     for (int i = 0; i < A.size() ; i++) {
  17.     l[i][0]=0;
  18.     a=l[i][0];
  19.     }
  20.     for (int j = 0; j < B.size() ; j++) {
  21.         l[0][j]=0;
  22.         a=l[0][j];
  23.     }
  24.     for (int i = 1; i < A.size() ; i++) {
  25.         for (int j = 1; j < B.size() ; j++) {
  26.             if (i == 0 || j == 0)
  27.                 l[i][j] = 0;
  28.             else if (A[i] == B[j])
  29.                 l[i][j] = l[i - 1][j - 1] + 1;
  30.             else
  31.                 l[i][j] = max(l[i - 1][j], l[i][j - 1]);
  32.             a=l[i][j];
  33.         }
  34.     }
  35.     return a;
  36. }
  37.  
  38. int main() {
  39.     string x;
  40.     string y;
  41.     int res;
  42.  
  43.     ifstream fin;
  44.     fin.open("input.txt");
  45.     if (fin.is_open()) {
  46.         getline(fin, x);
  47.         getline(fin, y);
  48.         fin.close();
  49.     }
  50.  
  51.     res = getLCS(x, y);
  52.  
  53.     fstream fout;
  54.     fout.open("output.txt", ios::out);
  55.     fout << res;
  56.     fout.close();
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement