-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuva531Compromise.cpp
More file actions
124 lines (109 loc) · 2.47 KB
/
uva531Compromise.cpp
File metadata and controls
124 lines (109 loc) · 2.47 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include<bits/stdc++.h>
#define pb(n) push_back(n)
#define bug cout<<"bug";
#define all(c) c.begin(),c.end()
#define fr(i,n) for(i=0;i<n;i++)
#define sf(n) scanf("%d",&n)
#define mst(n) memset(n,0,sizeof(n))
#define nl printf("\n")
#define lli long long int
#define tr(container,it) \
for (auto it = container.begin(); it != container.end() ; ++it)
#define INF INT_MAX
#define mp(i,j) make_pair(i,j)
#define mstn(n) memset(n,-1,sizeof(n))
#define infile freopen("in.txt","r",stdin)
#define outfile freopen("out.txt","w",stdout)
#define ull unsigned long long
#define V 1000001
#define sz 1005
#define MOD 100000007
using namespace std;
int mapping(string str,int &cnt,map<string,int> &imap)
{
if(imap.find(str)==imap.end())
{
imap[str] = cnt++;
}
return imap[str];
}
int person[2][100+1];
int dp[100+1][100+1];
vector<string> word(200);
int m,n;
int lcs(int m,int n)
{
for(int i=1; i<=n; i++)
{
for(int j=1; j<=m; j++)
{
if(person[0][j]==person[1][i])
{
dp[i][j] = dp[i-1][j-1] + 1;
}
else
{
dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
}
}
}
stack<string> st;
int r = n,c = m;
while(r!=0 && c!=0)
{
if(dp[r][c]==dp[r-1][c])
{
r = r-1;
}
else if(dp[r][c]==dp[r][c-1])
{
c= c-1;
}
else
{
st.push(word[person[0][c]]);
r = r-1, c = c-1;
}
}
string str = st.top();
cout<<str;
st.pop();
while(!st.empty())
{
str = st.top();
st.pop();
cout<<" "<<str;
}
nl;
}
int main()
{
string str;
while(cin>>str)
{
mst(dp), mst(person) ;
int cnt = 0,indx = 1;
map<string,int> imap;
int m = mapping(str,cnt,imap);
word[m] = str;
person[0][indx++] = m;
int f=1,s=0;
for(int p=0; p<2; p++)
{
while(cin>>str)
{
if(str=="#")
{
indx = 1;
break;
}
m = mapping(str,cnt,imap);
person[p][indx++] = m;
word[m] = str;
if(p==0) f++;
else s++;
}
}
lcs(f,s);
}
}