POJ 1226 Substrings

 

Substrings

Time Limit: 1000ms
Memory Limit: 10000KB
This problem will be judged on  PKU. Original ID: 1226
64-bit integer IO format: %lld      Java class name: Main
 
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 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 

Source

 
 
解题:霸蛮好了。。。
 
 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstring>

 4 #include <cmath>

 5 #include <algorithm>

 6 #include <climits>

 7 #include <vector>

 8 #include <queue>

 9 #include <cstdlib>

10 #include <string>

11 #include <set>

12 #include <stack>

13 #define LL long long

14 #define pii pair<int,int>

15 #define INF 0x3f3f3f3f

16 using namespace std;

17 string str[110];

18 int main() {

19     int t,i,j,k,n;

20     bool  flag;

21     scanf("%d",&t);

22     while(t--){

23         scanf("%d",&n);

24         for(i = 0; i < n; i++)

25             cin>>str[i];

26         sort(str,str+n);

27         flag = false;

28         for(k = str[0].length(); k; k--){

29             for(i = 0; i + k <= str[0].length(); i++){

30                 string a = str[0].substr(i,k);

31                 string b(a.rbegin(),a.rend());

32                 for(j = 1; j < n; j++)

33                     if(str[j].find(a) == -1 && str[j].find(b) == -1) break;

34                 if(j == n) {flag = true;break;}

35             }

36             if(flag) break;

37         }

38         flag?printf("%d\n",k):puts("0");

39     }

40     return 0;

41 }
View Code

 

你可能感兴趣的:(substring)