-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path10100 - Longest Match.cpp
78 lines (64 loc) · 1.3 KB
/
10100 - Longest Match.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
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <memory.h>
#include <algorithm>
#include <iomanip>
using namespace std;
int const N = 1001;
int dp[N][N];
string s, tmp;
vector<string> s1, s2;
int rec(int i = 0, int j = 0) {
if(i == (int)s1.size() || j == (int)s2.size())
return 0;
int &ret = dp[i][j];
if(ret != -1) return ret;
ret = 0;
if(s1[i] == s2[j])
return ret = rec(i + 1, j + 1) + 1;
return ret = max(rec(i + 1, j), rec(i, j + 1));
}
int main() {
for(int cnt = 1; getline(cin, s); ++cnt) {
s1.clear();
s2.clear();
if(s == "") {
getline(cin, s);
cout << setw(2) << cnt << ". Blank!" << endl;
continue;
}
s += ' ';
tmp = "";
for(int i = 0; i < (int)s.length(); ++i) {
if(isalpha(s[i]) || isdigit(s[i])) {
tmp += s[i];
} else {
if(tmp != "")
s1.push_back(tmp);
tmp = "";
}
}
getline(cin, s);
if(s == "") {
cout << setw(2) << cnt << ". Blank!" << endl;
continue;
}
s += ' ';
tmp = "";
for(int i = 0; i < (int)s.length(); ++i) {
if(isalpha(s[i]) || isdigit(s[i])) {
tmp += s[i];
} else {
if(tmp != "")
s2.push_back(tmp);
tmp = "";
}
}
memset(dp, -1, sizeof dp);
cout << setw(2) << cnt << ". Length of longest match: " << rec() << endl;
}
return 0;
}