-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
72 lines (54 loc) · 1.84 KB
/
Main.cpp
File metadata and controls
72 lines (54 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
using namespace std;
int n; // size of sequence x
vector<char> x; // sequence x
int m; // size of sequence y
vector<char> y; // sequence y
// We asume that n >= m always to use this function
vector<char> sequence; // will always store a new subsequence of maximum size m
vector<char> longestCommonSubsequence; // will always store the last answer that found maximum size m
void findLongestCommonSubsequence(int xi = 0, int yj = 0){ // O(m*2^m)
if(xi == n || yj == m){ // base case
return;
}
findLongestCommonSubsequence(xi, yj+1); // first call
int count = 0; // use to backtrack how many element added to the sequence.
int i = xi;
int j = yj;
while(i != n && j != m){
if(x[i] == y[j]){
count++;
sequence.push_back(x[i]);
j++;
findLongestCommonSubsequence(i+1, j); // second call
}
i++;
}
if(sequence.size() >= longestCommonSubsequence.size()){ // if the sequence is longer then the past sequence replace it.
longestCommonSubsequence.clear();
longestCommonSubsequence = sequence;
}
while(count > 0){ // clear the elements that had been added to the sequence
sequence.pop_back();
count--;
}
}
int main(){
clock_t z = clock();
cin >> n >> m;
x.resize(n);
y.resize(m);
for(int i = 0; i < n; i++){
cin >> x[i];
}
for(int i = 0; i < m; i++){
cin >> y[i];
}
findLongestCommonSubsequence();
// print the longest common subsequence
for(int i = 0; i < longestCommonSubsequence.size(); i++){
cout << longestCommonSubsequence[i] << " \n"[i == longestCommonSubsequence.size()-1];
}
cerr << "Run Time : " << ((double)(clock() - z)/CLOCKS_PER_SEC) << endl; // used to show the run time of using the code
return 0;
}