zoukankan      html  css  js  c++  java
  • Hdu 1867 KMP

    题目链接

    题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑

    思路: 查找的时候, 当文本串结束的时候, 返回匹配串的位 

    1 /*************************************************************************

     2     > File Name: 1867.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年05月15日 星期四 11时24分11秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <cstdio>
    12 #include <fstream>
    13 #include <cstring>
    14 #include <iostream>
    15 #include <algorithm>
    16 using namespace std;
    17 #define MAX_N (100000+5)
    18 
    19 char a[MAX_N], b[MAX_N];
    20 int f[MAX_N<<1];
    21 void getFail(char *);
    22 
    23 int 
    24 find (char *T, char *P) {
    25       int n = strlen(T);//m = strlen(P);
    26     getFail(P);
    27     int j = 0;
    28     for (int i = 0; i < n; i++) {
    29           while (j && T[i] != P[j]) j = f[j];
    30         if (P[j] == T[i]) j++;
    31     }
    32 
    33     return j;
    34 }
    35 
    36 void
    37 getFail(char *P) {
    38       int m = strlen(P);
    39     f[0] = f[1] = 0;
    40     for (int i = 1; i < m; i++) {
    41           int j = f[i];
    42         while (j && P[i] != P[j]) j = f[j];
    43         f[i+1] = P[i] == P[j] ? j+1 : 0;
    44     }
    45 }
    46 
    47 int
    48 main(void) {
    49       while (~scanf("%s %s", a, b)) {
    50         //    int len1 = strlen(a);
    51         //    int len2 = strlen(b);
    52             int cnt = find(a, b);
    53         //    cout << cnt << endl;
    54             int cnt2 = find(b, a);
    55         //    cout << cnt2 << endl;
    56             if (cnt > cnt2) {
    57                 printf("%s", a);
    58                 printf("%s
    ", b+cnt);
    59             } else if (cnt < cnt2){
    60                 printf("%s", b);
    61                 printf("%s
    ", a+cnt2);
    62             } else {
    63                   if (strcmp(a, b) < 0) {
    64                     printf("%s", a);
    65                     printf("%s
    ", b+cnt);
    66                 } else {
    67                       printf("%s", b);
    68                     printf("%s
    ", a+cnt2);
    69                 }
    70             }
    71     }
    72 
    73     return 0;
    74 }
  • 相关阅读:
    spring boot整合mybatis+mybatis-plus
    Spring Boot Shiro 权限管理
    Spring Boot 热部署(转)
    SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例
    详解Spring Boot配置文件之多环境配置
    Java 泛型-泛型类、泛型方法、泛型接口、通配符、上下限
    mybatis中整合ehcache缓存框架的使用
    Java总结篇系列:Java泛型
    java中接口之间的继承
    Java中接口继承泛型接口
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3729777.html
Copyright © 2011-2022 走看看