- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLCS.cpp
More file actions
Latest commit
160 lines (142 loc) · 3.47 KB
/
Copy pathLCS.cpp
File metadata and controls
160 lines (142 loc) · 3.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
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
/* -------------------------------- */
/* Name: MD. Khairul Basar */
/* Institute: HSTU */
/* Dept: CSE */
/* Email: khairul.basar93@gmail.com */
/* -------------------------------- */
#include<bits/stdc++.h>
/* all header files included */
#definefs first
#definesc second
#definespprintf("")
#definenlprintf("\n")
#definepb(a) push_back(a)
#definemp(a,b) make_pair(a,b)
#definesetzero(a) memset(a,0,sizeof(a))
#definesetneg(a) memset(a,-1,sizeof(a))
#definesetinf(a) memset(a,126,sizeof(a))
#definetc1(x) printf("Case %d: ",x)
#definetc2(x) printf("Case #%d: ",x)
#definetc3(x) printf("Case %d:\n",x)
#definetc4(x) printf("Case #%d:\n",x)
#definepr1(x) cout<<x<<"\n"
#definepr2(x,y) cout<<x<<""<<y<<"\n"
#definepr3(x,y,z) cout<<x<<""<<y<<""<<z<<"\n"
#definepr4(w,x,y,z) cout<<w<<""<<x<<""<<y<<""<<z<<"\n"
#definefastios::sync_with_stdio(0)
#definereadfreopen("input.txt","r",stdin)
#definewritefreopen("output.txt","w",stdout)
#defineprflag1(flag) printf("%s\n",(flag)?"YES":"NO")
#defineprflag2(flag) printf("%s\n",(flag)?"Yes":"No")
#defineprflag3(flag) printf("%s\n",(flag)?"yes":"no")
/* defining macros */
usingnamespacestd;
template <classT> inline T bigmod(T b, T p, T m)
{
T ret;
if(p==0) return1;
if(p&1)
{
ret=(bigmod(b,p/2,m)%m);
return ((b%m)*ret*ret)%m;
}
else
{
ret=(bigmod(b,p/2,m)%m);
return (ret*ret)%m;
}
}
/* template functions */
typedeflonglongLL;
typedefunsignedlonglongULL;
typedef pair<int, int>pii;
typedef pair<LL, LL>pll;
typedef vector<int>vi;
typedef vector<LL>vll;
typedef vector<pii>vpii;
typedef vector<pll>vpll;
/* type definition */
int dx4[]= {1,-1,0,0};
int dy4[]= {0,0,1,-1};
int dx6[]= {0,0,1,-1,0,0};
int dy6[]= {1,-1,0,0,0,0};
int dz6[]= {0,0,0,0,1,-1};
int dx8[]= {1,-1,0,0,-1,1,-1,1};
int dy8[]= {0,0,1,-1,1,1,-1,-1};
int dkx8[]= {-1,1,-1,1,-2,-2,2,2};
int dky8[]= {2,2,-2,-2,1,-1,1,-1};
/* direction array */
int tc=1;
constdouble eps=1e-9;
constdouble pi=acos(-1.0);
constlonglongint mx=1e3;
constlonglongint mod=1e9+7;
/* global declaration */
int dp[mx+5][mx+5];
string a,b,ans;
intlcs(int i, int j)
{
if(i==a.size() || j==b.size()) return0;
int &ret=dp[i][j];
if(ret!=-1) return ret;
ret=0;
if(a[i]==b[j]) ret=lcs(i+1,j+1)+1;
else ret=max(lcs(i+1,j),lcs(i,j+1));
return ret;
}
voidprint_lcs(int i, int j)
{
if(i==a.size() || j==b.size())
{
pr1(ans);
return;
}
if(a[i]==b[j])
{
ans+=a[i];
print_lcs(i+1,j+1);
}
else
{
if(dp[i+1][j]>dp[i][j+1]) print_lcs(i+1,j);
elseprint_lcs(i,j+1);
}
return;
}
voidprint_all_lcs(int i, int j)
{
if(i==a.size() || j==b.size())
{
pr1(ans);
return;
}
if(a[i]==b[j])
{
ans+=a[i];
print_all_lcs(i+1,j+1);
ans.erase(ans.end()-1);
}
else
{
if(dp[i+1][j]>dp[i][j+1]) print_all_lcs(i+1,j);
elseif(dp[i+1][j]<dp[i][j+1]) print_all_lcs(i,j+1);
elseprint_all_lcs(i+1,j),print_all_lcs(i,j+1);
}
return;
}
intmain()
{
while(cin>>a>>b)
{
setneg(dp);
pr1(lcs(0,0));
nl;
ans="";
print_lcs(0,0);
nl;
ans="";
print_all_lcs(0,0);
nl;
}
return0;
}