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. linux install nginx error

    1 2 3 4 5 6 7 8 9 10 11 [mahao01@127.0.0.1 nginx-1.2.9]$ make make -f objs/Makefile make[1]: Enterin ...

  2. 百度apistore第三方登陆

    最近做一个个人博客,其中的登陆模块我想使用第三方登陆来做.上网搜一下有好多例子,但是大多数都是一个网站的第三方登陆,如QQ.微博.人人,没有集成的组件,于是就在网上搜一下百度的apistore,百度果 ...

  3. JSTL入门

    在页面最上方引入 -------------------- if语句 8}"> b的值大于8 --------------------- foreach语句 i的值是:${i}

  4. easyui给select控件绑定change事件

    一般的做法是使用jQuery来绑定,例如: $("#id").change(function(){ alert("change事件绑定"); }); 当给sel ...

  5. ThinkPHP第二十五天(自动完成、用户名密码PHP正则、移位或加密函数)

    1.ThinkPHP自动完成功能 跟昨天的自动验证功能类似,也是需要在自定义的UserModel类,进行使用. 使用方法:定义$_auto属性 $_auto = array( array(完成字段,完 ...

  6. python 常用模块及方法

    ******************** PY核心模块方法 ******************** os模块: os.remove()         删除文件 os.unlink()        ...

  7. python基础学习笔记6--异常

    异常 1.什么是异常?  python用异常对象来表示( exception object)异常情况.如果异常没有被处理或扑捉,程序就会所谓的回溯(TraceBack,一种错误信息)而终止执行: 2. ...

  8. qt5集成libcurl实现tftp和ftp的方法一:搭建环境(五篇文章)

    最近使用QT5做一个软件,要求实现tftp和ftp文件传输,使用QT5开发好UI界面等功能,突然发现QT5不直接提供tftp和ftp支持,无奈之下只好找第三方库来间接实现,根据网友的介绍,libcur ...

  9. BASE64URL解析

    BASE64URL是一种在BASE64的基础上编码形成新的加密方式,为了编码能在网络中安全顺畅传输,需要对BASE64进行的编码,特别是互联网中. BASE64URL编码的流程: .明文使用BASE6 ...

  10. 夜未央Test1题解

    T1 积木游戏              树状数组的一个简单应用,建立一个维护左节点的树状数组和一个维护右节点的树状数组,对于add操作,只要在维护左节点的树状数组l处加1,维护右节点的树状数组r处加 ...