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. Android Listview切换动画,扩展到任意view切换之间动画实现

    添加布局如下: <?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2010 ...

  2. 排序算法——交换排序(冒泡排序、快速排序)(java)

    一.冒泡排序 时间复杂度:O(n^2) 公认最慢的排序,每次把最大/最小的放一边,原理: [57,68,59,52] [57,68,59,52] [57,59,68,52] [57,59,52,68] ...

  3. 物理DG主备库切换时遇到ORA-16139: media recovery required错误

    在物理DG主备库切换时遇到ORA-16139: media recovery required错误 SQL> ALTER DATABASE COMMIT TO SWITCHOVER TO PRI ...

  4. 今天碰到的angular 中的一个小坑

    最近在自个儿研究angular,在写一个demo的时候总是有问题,最后发现居然是大小写的问题,卧槽 特tm的坑爹了,代码如下: <!DOCTYPE html> <html lang= ...

  5. 虎记:强大的nth-child(n)伪类选择器玩法

    写在前面的戏: 最近参加了度娘前端IFE的春季班,刷任务,百度真是有营销头脑,让咱们这帮未来的技术狂人为他到处打广告(我可不去哪),其中做的几个任务中有几个以前没有用到的东西, 也算是有些许收获(现在 ...

  6. 全国三级城市联动 js版

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. ImageMagick 转换 progressive jpeg

    什么是渐进式图片(Progressive JPEG)? 来自 张鑫旭-鑫空间-鑫生活 的解释: 不知诸位有没有注意到,这些jpg格式的图片在呈现的时候,有两种方式,一种是自上而下扫描式的,还有一种就是 ...

  8. Git跨平台中文乱码临时解决方案

    Git 是一个非常优秀的分布式版本控制系统,最初为Linux Kernel版本管理进行量身定做.优点是,和其他版本控制系统相比,稳定,速度快,跨平台,易学易用,无需要花费成本.更多优点请点击阅读:ht ...

  9. ExtJS001HelloWorld弹窗

    html页面 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...

  10. ELK(ElasticSearch, Logstash, Log4j)系统日志搭建

    1.elk平台介绍 Elasticsearch是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logsta ...