Hdu 1867 KMP
题目意思: 给出两个字符串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的更多相关文章
- HDU 2087 HDU 1867 KMP标准模板题
贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...
- hdu 1867 kmp匹配
#include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Cyclic Nacklace HDU 3746 KMP 循环节
Cyclic Nacklace HDU 3746 KMP 循环节 题意 给你一个字符串,然后在字符串的末尾添加最少的字符,使这个字符串经过首尾链接后是一个由循环节构成的环. 解题思路 next[len ...
- HDU 1867 A + B for you again ----KMP
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...
- HDU 1867 A + B for you again KMP解决问题的方法
这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...
- A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)
Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...
- hdu 1867 求两个串的"和"最小 ,KMP
题意: 给你两个字符串,让你求str1+str2,就是把1的后面和2的前面重叠的地方只显示一遍就行了 abc + bcd = abcd,要求和的长度最小,和最小的前提下求字典序最小,还有就 ...
- 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 ...
随机推荐
- 【JZOJ5179】【NOI2017模拟6.29】哈哈
题意 给定一个长度为n的序列,你可以进行若干次操作: 选择一个区间,删掉,并获得Val[Len]的得分,Len为这个区间的长度: 其中这个区间满足: 1.相邻两个数差的绝对值为1 2.每个数都大于相邻 ...
- PAT甲级——A1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- istio1.1(openshift) 流量路由
1.准备测试应用 准备两个nginx Pod和一个proxy 创建应用 apiVersion: apps.openshift.io/v1 kind: DeploymentConfig metadata ...
- Wamp Apache 启动失败检测方法
一般情况下,看错误日志就可以解决.如果遇到错误日志看不到的情况,不放试试下面的方法 //无错误日志解决办法cmd命令行切换到C:\wamp\bin\apache\apache2.4.9\bin目录 输 ...
- 搭建php虚拟环境
参考网址: http://my.oschina.net/u/998304/blog/501363?p={{totalPage}} Box镜像列表: http://www.vagrantbox.es/ ...
- didFailWithError: Error Domain=kCLErrorDomain Code=0 “The operation couldn’t be completed. (kCLError
This error also occurs if you have Scheme/Edit Scheme/Options/Allow Location Simulation checked but ...
- spring cloud深入学习(四)-----eureka源码解析、ribbon解析、声明式调用feign
基本概念 1.Registe 一一服务注册当eureka Client向Eureka Server注册时,Eureka Client提供自身的元数据,比如IP地址.端口.运行状况指标的Uri.主页地址 ...
- JDBC 操作数据库实例
JDBC是什么 JDBC代表Java数据库连接(Java Database Connectivity),它是用于Java编程语言和数据库之间的数据库无关连接的标准Java API,换句话说:JDBC是 ...
- Spring事务_注解_特性
Spring 是一个 IOC 和 AOP 容器框架. ## 控制反转(IOC) ## 传统的 java 开发模式中,当需要一个对象时,我们会自己使用 new 或者 getInstance 等直接或者间 ...
- spring的基于xml的AOP配置案例和切入点表达式的一些写法
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...