A + B for you again

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3061    Accepted Submission(s): 755

Problem Description
Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
 
Input
For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
 
Output
Print the ultimate string by the book.
 
Sample Input
asdf sdfg
asdf ghjk
 
Sample Output
asdfg
asdfghjk

注意题目要求的是最短的字符串能包含所给的两个字符串,这里的包含一定是前一部分或后一部分包含,不能中间包含

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=100000+10;
char a[MAX],b[MAX];
int next[MAX]; void get_next(char *a,int len){
int i=-1,j=0;
next[0]=-1;
while(j<len){
if(i == -1 || a[i] == a[j]){
if(a[++i] == a[++j])next[j]=next[i];
else next[j]=i;
}else i=next[i];
}
} int KMP(char *a,char *b,int lena,int lenb){
get_next(a,lena);
int i=0,j=0;
while(i<lena && j<lenb){
if(i == -1 || a[i] == b[j])++i,++j;
else i=next[i];
}
if(i<lena || (i == lena && j == lenb))return i;//a不能是b中间部分的字串
return 0;
} int main(){
while(cin>>a>>b){
int lena=strlen(a),lenb=strlen(b);
int la=KMP(a,b,lena,lenb);
int lb=KMP(b,a,lenb,lena);
if(la>lb || (la == lb && strcmp(a,b)>0)){
cout<<b;
for(int i=la;i<lena;++i)cout<<a[i];
}
else{
cout<<a;
for(int i=lb;i<lenb;++i)cout<<b[i];
}
cout<<endl;
}
return 0;
}

hdu1867之KMP的更多相关文章

  1. 【KMP】hdu1867(A + B for you again) 杭电java a题真坑

    点击打开链接 Problem Description Generally speaking, there are a lot of problems about strings processing. ...

  2. KMP回顾学习

    记住这张图,getnext就是对一个已知的待匹配的串进行分析,nex[i]表示当a[i]匹配失败后我能跳到哪里,继续尝试匹配,而不是每一次失败都从头再来,先来看看代码 const int maxn = ...

  3. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  4. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  5. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  6. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  7. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

  8. KMP算法实现

    链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...

  9. KMP专题

    1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...

随机推荐

  1. hadoop搭建杂记:Linux下虚拟机集群网络搭建

    VirtualBox搭建hadoop伪分布式模式 VirtualBox搭建hadoop伪分布式模式 master: ip:192.168.56.120 机器名: master 启动NameNode 启 ...

  2. R与数据分析旧笔记(十五) 基于有代表性的点的技术:K中心聚类法

    基于有代表性的点的技术:K中心聚类法 基于有代表性的点的技术:K中心聚类法 算法步骤 随机选择k个点作为"中心点" 计算剩余的点到这个k中心点的距离,每个点被分配到最近的中心点组成 ...

  3. 15-C语言结构体

    目录: 一.大型软件开发 二.头文件和static 三.结构体 四.联合 五.枚举 回到顶部 一.大型软件开发 将一个代码中的内容,拆分成多个文件,最后的可执行文件只要一个. 操作步骤: 1 原来只有 ...

  4. mac定时任务

    <?xml version=”1.0″ encoding=”UTF-8″?><!DOCTYPE plist PUBLIC “-//Apple//DTD PLIST 1.0//EN” ...

  5. Core第三方开源Web框架

    NET Core第三方开源Web框架YOYOFx   YOYOFx框架 YOYOFx是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台. 本着学习的态度,造了这个 ...

  6. oracle查询字符集语句

      (1)查看字符集(三条都是等价的) 复制代码 代码如下: select * from v$nls_parameters  where parameter='NLS_CHARACTERSET'sel ...

  7. PHP解析和生成xml(DOMDocument版)

    上次和大家分享了SimpleXML操作xml的一些知识,但是php中除了simplexml还有DOMDocument,这次就着重来看看DOMDocument的用法,还是把生成xml和解析xml分开写 ...

  8. PHP_Yii框架_专辑<一>

    一.PHP主流框架 cakephp—速度比较慢.CI(codeIgniter)—小型.symfony. TP(thinkphp)—国人开发.小型.zendframework(官方)—大型 Yii: 特 ...

  9. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  10. divmod数字处理函数

    divmod(a,b)函数 中文说明: divmod(a,b)方法返回的是a//b(除法取整)以及a对b的余数 返回结果类型为tuple 参数: a,b可以为数字(包括复数) 版本: 在python2 ...