Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5654 Accepted Submission(s): 2487
Problem Description
You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings.
Input
The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains a single integer n (1 <= n <= 100), the number of given strings, followed by n lines, each representing one string of minimum length 1 and maximum length 100. There is no extra white space before and after a string.
Output
There should be one line per test case containing the length of the largest string found.
Sample Input
复制代码
1
2
3
4
5
6
7
8
9
10
11
122 3 ABCD BCDFF BRCD 2 rose orchid
Sample Output
复制代码
1
2
3
4
5
62 2
Author
Asia 2002, Tehran (Iran), Preliminary
复制代码
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/* 用string类型保存字符串,边读入的时候边找到最短的一个字符串 因为最大的重复字串长度最长也不可能最短的字符串长度 所以只需枚举最短的字符串的子串,判断是否都是别的字符串的子串, 求出最大长度即可 用到了string.find 和 string.rfind */ #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<set> #include<string> #include<iterator> using namespace std; int main (void) { int T , n; int i , j, k; scanf("%d", &T); while(T--) { int count = 0; int min = 100000; int len; int flag; vector<string>s; s.clear(); scanf("%d", &n); for(i = 0; i < n; i++) { string temp; cin >> temp; len = temp.size(); if(min > len) {min = len; flag = i;} s.push_back(temp); } for(i = s[flag].size(); i > 0; i--) //从最小的母串开始从长到短找子串 for(j = 0; j <= s[flag].size() - i; j++) //长度为i的子串在母串中找 { string temp1, temp2; //用temp1来保存字串 temp2来保存字串的反串 temp1 = s[flag].substr(j, i); temp2 = temp1; reverse(temp2.begin(),temp2.end()); //将temp2 反串 for(k = 0; k < n; k++) { if(k == flag) continue; if(s[k].find(temp1) == string::npos && s[k].rfind(temp2)) //当正反子串在母串中都未发现时即跳出 break; } if(k == n && count < temp1.size()) count = temp1.size(); } printf("%dn", count); } return 0; }
最后
以上就是精明香菇最近收集整理的关于HDU 1238 SubstringsSubstrings的全部内容,更多相关HDU内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复