1 #include <vector> 2 #include <string> 3 #include <iostream> 4 #include <algorithm> 5 using namespace std; 6 bool vv(const string a,const string b) 7 { 8 return a.length()<b.length(); 9 } 10 bool issub(string a,string b) 11 { 12 for (int i=0;i<=b.length()-a.length();i++) 13 { 14 bool is2=1; 15 for (int j=0;j < a.length();j++) 16 { 17 if (a[j]!=b[i+j]) 18 { 19 is2=0; 20 break; 21 } 22 } 23 if (is2) 24 return true; 25 } 26 return false; 27 } 28 29 int main() 30 { 31 int n;cin>>n; 32 while (n--) 33 { 34 int m; 35 cin>>m; 36 vector <string> v(m); 37 for (int i=0;i < m;i++ ) 38 { 39 cin>>v[i]; 40 } 41 sort (v.begin(),v.end(),vv); 42 int sum =0; 43 string s = v[0]; 44 for (int j=0;j < s.length();j++) 45 { 46 for (int i=0;i<s.length();i++) 47 { 48 if (i <= j) 49 { 50 string s2(s,i,s.length()-j); 51 bool bb=1; 52 for (int p=1;p < m;p++) 53 { 54 string vpr=v[p]; 55 reverse(vpr.begin(),vpr.end()); 56 if (!issub(s2,v[p]) && !issub(s2,vpr)) 57 { 58 bb=0; 59 break; 60 } 61 } 62 if (bb) 63 { 64 sum = s2.length(); 65 goto fuck; 66 } 67 } 68 } 69 } 70 fuck: 71 cout<<sum<<endl; 72 } 73 return 0; 74 }
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1238
Substrings
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5684 Accepted Submission(s): 2503
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
2 3 ABCD BCDFF BRCD 2 rose orchid
Sample Output
2 2
Author
Asia 2002, Tehran (Iran), Preliminary
其实就是求最大子串的,第一个进blog的题。。。先字符串长度升序排序,然后在最短的里面找子串就行了。
代码编写的比较乱,goto都用上去了~。~ 这道题尼玛做了几小时有木有啊,(其实半小时就出来了,然后WA,然后调试调试调试调试~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~最后发现少了个等号!!尼玛坑爹啊有木有,一个下午的时间都TM没了)
浙公网安备 33010602011771号