-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPOJ1226(AC).cpp
103 lines (91 loc) · 1.55 KB
/
POJ1226(AC).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
94
95
96
97
98
99
100
101
102
103
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int n;
vector<string> v;
bool suc;
int max_len;
void reverse_string(char s[])
{
int i, len;
char ch;
if(!s){
return;
}
len = strlen(s);
i = 0;
while(i < len - 1 - i){
ch = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = ch;
++i;
}
}
bool check_common_substr(const string &sub)
{
int i;
char s[500];
for(i = 1; i < (int)v.size(); ++i){
if(v[i].find(sub) == string::npos){
strcpy(s, sub.data());
reverse_string(s);
if(v[i].find(string(s)) == string::npos){
return false;
}
}
}
return true;
}
bool comparator(const string &a, const string &b)
{
if(a.size() == b.size()){
return a < b;
}else{
return a.size() < b.size();
}
}
int main()
{
int i, j;
int t, ti;
char s[500];
string sub, str;
while(scanf("%d", &t) == 1){
for(ti = 0; ti < t; ++ti){
scanf("%d", &n);
v.clear();
for(i = 0; i < n; ++i){
scanf("%s", s);
v.push_back(string(s));
}
sort(v.begin(), v.end(), comparator);
if(n == 1){
printf("%d\n", v[0].size());
continue;
}
suc = false;
strcpy(s, v[0].data());
str = string(s);
for(i = str.size(); !suc && i > 0; --i){
for(j = 0; !suc && j + i <= (int)str.size(); ++j){
sub = str.substr(j, i);
suc = check_common_substr(sub);
if(suc){
max_len = (int)sub.size();
}
}
}
if(suc){
printf("%d\n", max_len);
}else{
printf("0\n");
}
}
}
return 0;
}