【HDOJ】1238 Substrings

深搜+剪枝,简单字符串。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define MAXLEN 105
 5 #define MAXNUM 105
 6 
 7 char buf[MAXNUM][MAXLEN];
 8 char src1[MAXLEN], src2[MAXLEN];
 9 int case_n, n;
10 
11 int f(int index, int len) {
12     int max = 0;
13     int i=0, j, k;
14     char *p;
15 
16     while (i+max < len) {
17         for (j=0; j<=max; ++j)
18             src1[j] = src2[max-j] = buf[index][i+j];
19         src1[max+1] = src2[max+1] = '\0';
20         k = 1;
21         for (j=0; j<n; ++j) {
22             p = strstr(buf[j], src1);
23             if (p == NULL)
24                 p = strstr(buf[j], src2);
25             if (p == NULL) {
26                 k = 0;
27                 break;
28             }
29         }
30         if (k)
31             ++max;
32         else
33             ++i;
34     }
35 
36     return max;
37 }
38 
39 int main() {
40     int i, j, tmp, len;
41 
42     scanf("%d", &case_n);
43 
44     while (case_n--) {
45         scanf("%d", &n);
46         tmp = MAXLEN;
47         for (i=0; i<n; ++i) {
48             scanf("%*c%s", buf[i]);
49             len = strlen(buf[i]);
50             if (len < tmp) {
51                 tmp = len;
52                 j = i;
53             }
54         }
55         tmp = f(j, tmp);
56         printf("%d\n", tmp);
57     }
58 
59     return 0;
60 }

 

posted on 2014-04-18 18:09  Bombe  阅读(151)  评论(0编辑  收藏  举报

导航