-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva531.cpp
109 lines (87 loc) · 1.47 KB
/
uva531.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
vector<string> text1, text2;
int m, n;
int dp[110][110];
vector<string> lcs()
{
int m = text1.size();
int n = text2.size();
memset(dp, 0, sizeof(dp));
for(int i = 0; i <= m; i++)
{
for(int j = 0; j <=n; j++)
{
if(i == 0 || j == 0)
dp[i][j] = 0;
else if(text1[i-1] == text2[j-1])
dp[i][j] = 1 + dp[i-1][j-1];
else
dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
}
}
/* reconstruct lcs */
vector<string> rez;
rez.resize(dp[m][n]);
int i = m, j = n;
int index = dp[m][n];
while(i > 0 && j > 0)
{
if(text1[i-1].compare(text2[j-1]) == 0)
{
rez[index-1] = text1[i-1];
index--; i--; j--;
}
else if(dp[i-1][j] > dp[i][j-1])
i--;
else
j--;
}
return rez;
}
int main()
{
string s1, s2;
while(true)
{
text1.clear(); text2.clear();
cin >> s1;
if(cin.eof())
break;
text1.push_back(s1);
while(cin >> s1)
{
if(s1 == "#")
break;
text1.push_back(s1);
}
while(cin >> s2)
{
if(s2 == "#")
break;
text2.push_back(s2);
}
/*for(int i = 0; i < text1.size(); i++)
{
cout << text1[i] << endl;
}*/
vector<string> rez = lcs();
//cout << rez.size() << endl;
for(int i = 0; i < rez.size(); i++)
{
if(i != rez.size() - 1)
cout << rez[i] << " ";
else
cout << rez[i] << endl;
}
}
return 0;
}