Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdlib.h>
- using namespace std;
- int getLCS(string &A, string &B) {
- A=" "+A;
- B=" "+B;
- int l[A.size() + 1][B.size() + 1];
- int a;
- l[0][0]=0;
- for (int i = 0; i < A.size() ; i++) {
- l[i][0]=0;
- a=l[i][0];
- }
- for (int j = 0; j < B.size() ; j++) {
- l[0][j]=0;
- a=l[0][j];
- }
- for (int i = 1; i < A.size() ; i++) {
- for (int j = 1; j < B.size() ; j++) {
- if (i == 0 || j == 0)
- l[i][j] = 0;
- else if (A[i] == B[j])
- l[i][j] = l[i - 1][j - 1] + 1;
- else
- l[i][j] = max(l[i - 1][j], l[i][j - 1]);
- a=l[i][j];
- }
- }
- return a;
- }
- int main() {
- string x;
- string y;
- int res;
- ifstream fin;
- fin.open("input.txt");
- if (fin.is_open()) {
- getline(fin, x);
- getline(fin, y);
- fin.close();
- }
- res = getLCS(x, y);
- fstream fout;
- fout.open("output.txt", ios::out);
- fout << res;
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement