Skip to content

Commit 6d80f6f

Browse files
authored
Update LCS_.cpp
1 parent 6b26d81 commit 6d80f6f

File tree

1 file changed

+51
-72
lines changed

1 file changed

+51
-72
lines changed

LCS_.cpp

Lines changed: 51 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,81 @@
1-
/*************************************
1+
/* _
2+
* _|_ o._ o._ __|_| _. _|_
3+
* _>| ||| ||| |(_|| |(_|_>| |
4+
* _|
5+
* @author Amirul islam Al Mamun
26
* An Easy LCS LightOJ - 1110 >> DP
3-
* @author Amirul Islam (shiningflash)
4-
************************************/
7+
*/
58

6-
#include <cstdio>
7-
#include <iomanip>
8-
#include <cstring>
9-
#include <cmath>
10-
#include <cstdlib>
11-
#include <cctype>
12-
#include <algorithm>
13-
#include <string>
14-
#include <vector>
15-
#include <queue>
16-
#include <map>
17-
#include <set>
18-
#include <sstream>
19-
#include <stack>
20-
#include <list>
21-
#include <iostream>
22-
#include <assert.h>
23-
24-
#define mem(x,y) memset(x,y,sizeof(x))
25-
#define CLEAR(x) memset(x,0,sizeof(x))
26-
27-
#define pb push_back
28-
#define Sort(v) sort(v.begin(),v.end())
29-
#define _sort(s, n) sort(s, s+n)
30-
#define sqr(x) ((x)*(x))
31-
32-
#define le 50001
33-
#define ERR 1e-9
34-
#define pi (2*acos(0))
35-
#define PI 3.141592653589793
36-
37-
#define scanint(a) scanf("%d",&a)
38-
#define scanLLD(a) scanf("%lld",&a)
39-
40-
typedef unsigned long long ll;
9+
#include <bits/stdc++.h>
4110
using namespace std;
4211

43-
/**********************End*******************/
44-
45-
string s1, s2;
46-
int cost[105][105], path[105][105], lcs[105];
47-
int t, l1, l2, lcs_len;
12+
const int mx = 1e3;
4813

49-
inline void LCS_LENGTH() {
50-
for (int i = 1; i <= l1; cost[i][0] = 0, i++);
51-
for (int j = 1; j <= l2; cost[0][j] = 0, j++);
14+
int path[mx][mx], cost[mx][mx];
5215

53-
for (int i = 1; i <= l1; i++) {
54-
for (int j = 1; j <= l2; j++) {
16+
int lcs(string s1, string s2) {
17+
for (int i = 1; i <= s1.length(); i++) {
18+
for (int j = 1; j <= s2.length(); j++) {
19+
// common word
5520
if (s1[i-1] == s2[j-1]) {
5621
cost[i][j] = cost[i-1][j-1] + 1;
5722
path[i][j] = 1;
5823
}
24+
// UP greater
5925
else if (cost[i-1][j] >= cost[i][j-1]) {
6026
cost[i][j] = cost[i-1][j];
6127
path[i][j] = 2;
6228
}
29+
// LEFT greater
6330
else {
6431
cost[i][j] = cost[i][j-1];
6532
path[i][j] = 3;
6633
}
6734
}
6835
}
69-
lcs_len = cost[l1][l2];
36+
return cost[s1.length()][s2.length()];
7037
}
7138

72-
inline void LCS() {
73-
int i = l1, j = l2, k = lcs_len-1;
74-
while (k >= 0) {
39+
string get_lcs_string(string s1, string s2, int lcs_len) {
40+
// no common substring
41+
if (lcs_len == 0) {
42+
return ":(";
43+
}
44+
45+
int i = s1.length();
46+
int j = s2.length();
47+
int k = lcs_len;
48+
49+
stack <char> st;
50+
51+
while (k > 0) {
7552
if (path[i][j] == 1) {
76-
lcs[k] = s1[i-1];
77-
i--; j--; k--;
53+
st.push(s1[i-1]);
54+
i--;
55+
j--;
56+
k--;
57+
}
58+
else if (path[i][j] == 2) {
59+
i--;
60+
}
61+
else {
62+
j--;
7863
}
79-
else if (path[i][j] == 2) i--;
80-
else if (path[i][j] == 3) j--;
8164
}
82-
}
8365

84-
inline void LCS_PRINT() {
85-
if (lcs_len <= 0) cout << ":(";
86-
else
87-
for (int i = 0; i < lcs_len; i++)
88-
cout << (char) lcs[i];
89-
cout << endl;
66+
string lcs_str = "";
67+
while (!st.empty()) {
68+
lcs_str.push_back((char) st.top());
69+
st.pop();
70+
}
71+
72+
return lcs_str;
9073
}
9174

9275
int main() {
93-
scanint(t);
94-
for (int i = 1; i <= t; i++) {
95-
cin >> s1 >> s2;
96-
l1 = s1.length(); l2 = s2.length();
97-
LCS_LENGTH();
98-
LCS();
99-
cout << "Case " << i << ": ";
100-
LCS_PRINT();
101-
}
76+
string s1, s2;
77+
cin >> s1 >> s2;
78+
int lcs_len = lcs(s1, s2);
79+
cout << get_lcs_string(s1, s2, lcs_len) << endl;
80+
return 0;
10281
}

0 commit comments

Comments
 (0)