HDU_1238——最大子串搜索

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
 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <windows.h>

 5 void sort(char (*str)[101]);  //给字符串序列按长度排序 

 6 int fun(char (*str)[101]);   //求发现的最大的子串的长度 

 7 //char* in_turn(char* s);    //字符串逆序 

 8 

 9 int main()

10 {

11    int t,n;

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

13    while(t--)   //提前判断范围 

14       {

15          int i=0;

16          char str[100][101]={0};

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

18          while(i<n)

19             {               

20                scanf("%s",str[i++]);   //gets会接收一个换行符 

21             }

22          sort(str);

23          printf("%d\n",fun(str));  

24       } 

25    system("pause>nul");

26    return 0;

27 }

28 

29 int fun(char (*str)[101])

30 {

31    int max=0,i,j,k;

32    char s[101];

33    for(k=0;str[0][k];k++)

34    {

35       for(i=1;i<=strlen(str[0])-k;i++)

36          {

37            if(max>i) continue;

38            int flag=1;

39            strcpy(s,str[0]+k);//i-k+1

40            s[i]='\0';        

41            for(j=1;str[j][0];j++)

42                {

43                if(strstr(str[j],s)==NULL && strstr(str[j],strrev(s))==NULL)  //返回一个指向s1中第一次出现s2中字符串的位置,没有返回NULL  

44                   {flag=0;break;}                                            //strrev()逆序函数 

45                }

46             if(flag==1)   

47                max=i;

48          }

49    }

50    return max;

51 }

52 /*

53 char* in_turn(char* str)

54 {

55    char _str[101];

56    int i;

57    for(i=0;str[i];i++)

58       {

59          _str[strlen(str)-1-i] = str[i];   

60       }

61    _str[strlen(str)]='\0';

62    return _str;

63 }

64 */

65 void sort(char (*str)[101])   //一个指向100个char类型的指针 

66 {

67    int i,j;

68    char temp[100];

69    for(i=0;str[i+1][0]!='\0';i++)

70       for(j=i+1;str[j][0]!='\0';j++)

71          {

72             if(strlen(str[i]) > strlen(str[j]))

73                {

74                   strcpy(temp,str[i]);

75                   strcpy(str[i],str[j]);

76                   strcpy(str[j],temp);

77                }   

78          }

79 }

 

你可能感兴趣的:(HDU)