poj 1226 Substrings

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5215 Accepted: 1730

本题是简单的字符串匹配,不过我却在这里贡献了好多次的Wronganswer,一开始是一直是CE,后来听别人说不支持strrev,但是我却在北大上找到一种编译器GCC可以支持strrev,但是我却愚蠢的竟然忽略了一种情况,当找不到时应该输出0.算是找到教训了吧!

代码:

 

  
    
1 #include < stdio.h >
2 #include < string .h >
3   char a[ 105 ][ 105 ],n;
4   int lcs( char substr[], int len)
5 {
6 int mark,i,j,len1;
7 char str1[ 105 ],str2[ 105 ];
8 len1 = len;
9 while (len1 > 0 )
10 {
11 for (i = 0 ;i <= len - len1;i ++ )
12 {
13 mark = 1 ;
14 strncpy(str1,substr + i,len1);
15 str1[len1] = ' \0 ' ;
16 strcpy(str2,str1);
17 strrev(str2);
18 for (j = 1 ;j <= n;j ++ )
19 {
20 if (strstr(a[j],str1) == NULL && strstr(a[j],str2) == NULL)
21 {
22 mark = 0 ;
23 break ;
24 }
25 }
26 if (mark)
27 {
28 printf( " %d\n " ,strlen(str1));
29 return 1 ;
30 }
31 }
32 len1 -- ;
33 }
34 return 0 ;
35 }
36
37 int main()
38 {
39 int t,i,min,len;
40 char substr[ 105 ];
41 scanf( " %d " , & t);
42 while (t -- )
43 {
44 scanf( " %d " , & n);
45 min = 105 ;
46 for (i = 1 ;i <= n;i ++ )
47 {
48 scanf( " %s " ,a[i]);
49 len = strlen(a[i]);
50 if (len < min)
51 {
52 min = len;
53 strcpy(substr,a[i]);
54 }
55 }
56 if ( ! lcs(substr,min))
57 printf( " 0\n " );
58
59 }
60 return 0 ;
61 }

 

你可能感兴趣的:(substring)