OpenJudge/Poj 1226 Substrings

1.链接地址:

http://bailian.openjudge.cn/practice/1226/

http://poj.org/problem?id=1226

2.题目:

总时间限制:
1000ms
内存限制:
65536kB
描述
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.
输入
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.
输出
There should be one line per test case containing the length of the largest string found.
样例输入
2

3

ABCD

BCDFF

BRCD

2

rose

orchid
样例输出
2

2 
来源
Tehran 2002 Preliminery

3.思路:

 

4.代码:

 1 #include <iostream>

 2 #include <cstdio>

 3 #include <cstdlib>

 4 #include <cstring>

 5 #include <string.h>

 6 using namespace std;

 7 const int NUM = 100;

 8 char strs[NUM][NUM + 1];

 9 int main()

10 {

11     //freopen("F:\\input.txt","r",stdin);

12     int i,j,k;

13 

14     int t;

15     cin>>t;

16     

17     int n,length;

18     while(t--)

19     {

20         cin>>n;

21         cin.get();

22         

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

24         {

25             scanf("%s",strs[i]);

26         }

27         

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

29 

30         length = strlen(strs[0]);

31         char substr[NUM + 1],substr2[NUM + 1];

32         int res = 0;

33         for(i = 1; i <= length; i++)

34         {

35             for(j = 0; (j+i-1) < length; j++)

36             {

37                 strncpy(substr,&strs[0][j],i);

38                 substr[i] = '\0';

39                 strcpy(substr2,substr);

40                 

41                 for(k = 0; k < (i+1)/2; k++)

42                 {

43                     char tmp = substr2[k];

44                     substr2[k] = substr2[i-1-k];

45                     substr2[i-1-k] = tmp;

46                 }

47                 

48                 

49                 //cout<<"substr="<<substr<<",substr2="<<substr2<<endl;

50                 for(k = 1; k < n; k++)

51                 {

52                     if(!strstr(strs[k],substr) && !strstr(strs[k],substr2)) break;

53                 }

54                 if(k >= n ) 

55                 {

56                     res = i;

57                     break;

58                 }

59             }

60         }

61         

62         cout<<res<<endl;

63     }

64     return 0;

65 }

 

你可能感兴趣的:(substring)