-
Notifications
You must be signed in to change notification settings - Fork 34
/
1110-An-Easy-LCS.cpp
101 lines (66 loc) · 1.11 KB
/
1110-An-Easy-LCS.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
#include <iostream>
#include <stdio.h>
using namespace std;
string minim(string x, string y)
{
if(x.length() < y.length()) {
return y;
}
if(x.length() > y.length()) {
return x;
}
for (int i = 0; i < x.length(); i++) {
if(x[i] < y[i]) {
return x;
}
if(x[i] > y[i]) {
return y;
}
}
return x;
}
int main()
{
int t;
string x;
string y;
string temp;
char xx[110];
char yy[110];
char ans[110];
int n;
int m;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
string a[110][110];
scanf("%s", xx);
scanf("%s", yy);
x = xx;
y = yy;
n = x.length();
m = y.length();
for (int i = n; i >= 0; i--) {
for (int j = m; j >= 0; j--) {
if(i == n or j == m) {
a[i][j] = "";
continue;
}
if(x[i] == y[j]) {
a[i][j] = x[i] + a[i+1][j+1];
continue;
}
a[i][j] = minim(a[i+1][j],a[i][j+1]);
}
}
for (int i = 0; i < a[0][0].length(); i++) {
ans[i] = a[0][0][i];
}
ans[a[0][0].length()] = '\0';
if(a[0][0].length() != 0) {
printf("Case %d: %s\n", cs, ans);
}
else {
printf("Case %d: :(\n", cs);
}
}
}