forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOJ3080.cpp
More file actions
76 lines (69 loc) · 1.61 KB
/
POJ3080.cpp
File metadata and controls
76 lines (69 loc) · 1.61 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
/*
POJ 3080 Blue Jeans
KMP
*/
#include <iostream>
#include <vector>
#include <string>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
vector<int> pi;
void build(string pattern) {
int lp = pattern.length();
pi.assign(lp, -1);
vector<int> pi(lp, -1);
int k = -1;
FOR(q,1,lp-1) {
while (k >= 0 && pattern[k + 1] != pattern[q]) k = pi[k];
if (pattern[k + 1] == pattern[q]) ++k;
pi[q] = k;
}
}
bool StringMatchKMP(string text, string pattern) {
int lt = text.length(), lp = pattern.length();
int k = -1;
REP(i,lt) {
while (k >= 0 && pattern[k + 1] != text[i]) k = pi[k];
if (pattern[k + 1] == text[i]) ++k;
if (k == lp - 1) return true;
}
return false;
}
void run() {
int n;
cin >> n;
vector<string> mm(n);
REP(i,n) cin >> mm[i];
RFOR(len,60,3) {
string seq = "";
REP(i,60-len+1) {
string pat = mm[0].substr(i,len);
build(pat);
bool match = true;
FOR(j,1,n-1) {
if (!StringMatchKMP(mm[j], pat)) {
match = false;
break;
}
}
if (match) {
if (seq == "" || pat < seq) seq = pat;
}
}
if (seq != "") {
cout << seq << endl;
return;
}
}
cout << "no significant commonalities" << endl;
}
int main() {
int kase;
cin >> kase;
while (kase--) {
run();
}
return 0;
}