题目链接

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

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

/*************************************************************************

     > File Name: 1867.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年05月15日 星期四 11时24分11秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_N (100000+5) char a[MAX_N], b[MAX_N];
int f[MAX_N<<];
void getFail(char *); int
find (char *T, char *P) {
int n = strlen(T);//m = strlen(P);
getFail(P);
int j = ;
for (int i = ; i < n; i++) {
while (j && T[i] != P[j]) j = f[j];
if (P[j] == T[i]) j++;
} return j;
} void
getFail(char *P) {
int m = strlen(P);
f[] = f[] = ;
for (int i = ; i < m; i++) {
int j = f[i];
while (j && P[i] != P[j]) j = f[j];
f[i+] = P[i] == P[j] ? j+ : ;
}
} int
main(void) {
while (~scanf("%s %s", a, b)) {
// int len1 = strlen(a);
// int len2 = strlen(b);
int cnt = find(a, b);
// cout << cnt << endl;
int cnt2 = find(b, a);
// cout << cnt2 << endl;
if (cnt > cnt2) {
printf("%s", a);
printf("%s\n", b+cnt);
} else if (cnt < cnt2){
printf("%s", b);
printf("%s\n", a+cnt2);
} else {
if (strcmp(a, b) < ) {
printf("%s", a);
printf("%s\n", b+cnt);
} else {
printf("%s", b);
printf("%s\n", a+cnt2);
}
}
} return ;
}

Hdu 1867 KMP的更多相关文章

  1. HDU 2087 HDU 1867 KMP标准模板题

    贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...

  2. hdu 1867 kmp匹配

    #include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...

  3. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  4. Cyclic Nacklace HDU 3746 KMP 循环节

    Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...

  5. HDU 1867 A + B for you again ----KMP

    题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...

  6. HDU 1867 A + B for you again KMP解决问题的方法

    这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...

  7. A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

  8. hdu 1867 求两个串的"和"最小 ,KMP

    题意:       给你两个字符串,让你求str1+str2,就是把1的后面和2的前面重叠的地方只显示一遍就行了 abc + bcd = abcd,要求和的长度最小,和最小的前提下求字典序最小,还有就 ...

  9. HDU 1867 A + B for you again(KMP算法的应用)

    A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. PAT甲级——A1015 Reversible Primes

    A reversible prime in any number system is a prime whose "reverse" in that number system i ...

  2. Composer安装Yii2以及相关扩展

    1.安装redis扩展 命令:composer require yiisoft/yii2-redis Git地址:https://github.com/yiisoft/yii2-redis/blob/ ...

  3. PHP基于openssl实现的非对称加密操作

    使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和php的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...

  4. jdk 数组位移运算

    1.采用先shift=31-Integer.numberOfLeadingZeros(scale);取int前面的补零个数31再减去拿到占得内存位长度 2.i偏移shift(其实等于I*位数) 加上b ...

  5. spring定时任务scheduler集群环境下指定运行服务器防止多服务器多次执行

    使用spring的@Scheduler注解可以非常方便的启动一个定时任务,但是当服务部署在多台服务器上做负载均衡的时候,可能会出现重复执行的情况. 现在我们通过代码指定job只在某一台机器执行. 首先 ...

  6. TZ_05_Spring_Transaction的纯注解开发

    1.数据库配置 jdbcConfiguation.java 1>使用Spring的EL表达式配合@Value()注解 @Value("${jdbc.Driver}") pri ...

  7. tr的display属性出现td的colspan无效问题

    http://www.aichengxu.com/other/9262680.htm 今天在做项目的时候发现用javascript控制 tr 的显示隐藏时,当把tr的显示由“display:none” ...

  8. 惊!VUE居然数据不能驱动视图?$set详细教程

    众所周知.VUE最大的优点就是数据驱动视图.当数据发生改变时,会监听到变化,后渲染到页面上.那么为什么当我们在修改data中声明的数组或对象时.VUE并没有监听到变化呢?这个我也不知道.我们可以后续再 ...

  9. 「LOJ10104」「一本通 3.6 练习 5」Blockade-Tarjan

    首先%%%lydrainbowcat 这个题唔,真是Tarjan好题(?) 不知道为啥也叫BLO,首字母? <题面> 搜索树真真的棒! 仔细看看,知道是Tarjan了,切掉一个点有不通的对 ...

  10. Weekly 10 小结

    A题 模拟 T = int(input()) while T: T -= 1 s = raw_input() n = len(s) res, pre = 0, 0 for i in xrange(1, ...