• 【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] = '';
    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
    ", tmp);
    57     }
    58 
    59     return 0;
    60 }
  • 相关阅读:
    初识计算机
    前端html css
    mysql高级
    mysql多表查询
    mysql数据库查询
    mysql表关系
    mysql数据类型
    mysql数据库介绍
    异步回调 协程
    GIL-全局解释器锁
  • 原文地址:https://www.cnblogs.com/bombe1013/p/3673879.html
Copyright © 2020-2023  润新知