forked from chinoxyz/Spoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADFRUITS.cpp
175 lines (148 loc) · 2.97 KB
/
ADFRUITS.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <cstdio>
#include <algorithm>
#include<iostream>
#include<vector>
#include<climits>
#include <complex>
#include <iostream>
#include <valarray>
#include<cstring>
#include<queue>
#include<bitset>
#include<map>
#include<set>
#include<sstream>
#include<ctime>
#include <stack>
#include <iomanip>
#include <cassert>
using namespace std;
#define PB push_back
#define i64 long long
#define FOR(i,a,b) for(i=(a);i<(b);++i)
#define FORE(i,a,b) for(i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,n)
#define REPE(i,n) FORE(i,0,n)
#define SZ(v) ((v).size())
#define LD long double
#define VI vector<int>
#define VS vector<string>
#define VD vector<double>
#define VLD vector<LD>
#define MSET(x,y) memset((x),(y),sizeof(x))
#define SZOF(x) sizeof((x))
#define VVI vector< VI >
#define PII pair< int,int >
#define VPII vector< PII >
#define MP make_pair
#define PIPII pair<int, PII >
#define PIPIPII pair< int, PIPII >
#define u64 unsigned i64
#define Vi64 vector<i64>
#define Vu64 vector<u64>
#define MAXN 105
int prex[MAXN][MAXN];
int prey[MAXN][MAXN];
int dp[MAXN][MAXN];
//bool included[MAXN][MAXN];
char as[MAXN];
char bs[MAXN];
int la,lb;
VI xs,ys;
int x,y,tx,ty,len,stx,sty;
void lcs()
{
xs.clear();
ys.clear();
MSET(prex,0);
MSET(prey,0);
MSET(dp,0);
//MSET(included,0);
la=strlen(as);
lb=strlen(bs);
for(int i=1;i<=la;++i)
{
for(int j=1;j<=lb;++j)
{
if(as[i-1]==bs[j-1])
{
dp[i][j]=1+dp[i-1][j-1];
prex[i][j]=i-1;
prey[i][j]=j-1;
}
else
{
if(dp[i-1][j]>dp[i][j-1])
{
dp[i][j]=dp[i-1][j];
prex[i][j]=i-1;
prey[i][j]=j;
}
else
{
dp[i][j]=dp[i][j-1];
prex[i][j]=i;
prey[i][j]=j-1;
}
}
}
}
x=la;
y=lb;
while(x && y)
{
if(as[x-1]==bs[y-1])
{
xs.PB(x-1);
ys.PB(y-1);
}
tx=prex[x][y];
ty=prey[x][y];
x=tx;
y=ty;
}
len=xs.size();
stx=-1;
sty=-1;
//printf("%d\n",len);
for(int i=len-1;i>=0;--i)
{
++stx;
while(stx!=xs[i])
{
printf("%c",as[stx]);
++stx;
}
++sty;
while(sty!=ys[i])
{
printf("%c",bs[sty]);
++sty;
}
printf("%c",as[stx]);
}
++stx;
while(stx!=la)
{
printf("%c",as[stx]);
++stx;
}
++sty;
while(sty!=lb)
{
printf("%c",bs[sty]);
++sty;
}
printf("\n");
}
int main()
{
freopen("Text/ADFRUITS.txt","r",stdin);
while(scanf("%s",as)!=EOF)
{
scanf("%s",bs);
//printf("%s\t%s\n",as,bs);
lcs();
}
return 0;
}