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

题目大意:
A+B,指的是 A的字符串 B的字符串;
例如:AAA: asdf;  BBB: sdfg;

因为 asdf 和 sdfg 中有相同最大后缀,和最大前缀 sdf;所以最终结果为 asdfg;

但是如果是 A: abcda ;B: bcdef;
因为 A的最大后缀与 B 的最大前缀不相同;所以最终结果为 abcdabcdef;
也就是说求出 A前缀和 B 后缀的最大公共子缀即可;可用 kmp 算法;

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxx=;
int p[maxx];
char a[maxx],b[maxx];
void pre(char b[])
{
int m=strlen(b);
int i=,j=;
p[]=-;
while(i<m)
{
if(j==-||b[i]==b[j])
{
i++,j++;
p[i]=j;
}
else
j=p[j];
}
}
int kmp(char a[],char b[])
{
int n=strlen(a);
int m=strlen(b);
pre(b);
int i=,j=;
while(i<n&&j<m)
{
if(j==-||a[i]==b[j])
{
i++,j++;
}
else j=p[j];
}
if(i==n)
return j;
else
return ;
}
int main()
{
while(~scanf("%s%s",a,b))
{
int x=kmp(a,b);
int y=kmp(b,a);
if(x==y)
{
if(strcmp(a,b)>)
{
printf("%s",b);
printf("%s",a+x);
}
else
{
printf("%s",a);
printf("%s",b+x);
}
}
else if(x>y)
{
printf("%s",a);
printf("%s",b+x);
}
else
{
printf("%s",b);
printf("%s",a+y);
}
printf("\n");
}
return ;
}

A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)的更多相关文章

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

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

  2. hdu 1403 Longest Common Substring(最长公共子字符串)(后缀数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=1403 Longest Common Substring Time Limit: 8000/4000 MS (Ja ...

  3. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  4. hdu 4300 Clairewd’s message(具体解释,扩展KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 Problem Description Clairewd is a member of FBI. ...

  5. hdu 1867 A + B for you again

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1867 A + B for you again Description Generally speaki ...

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

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

  7. Hdu 1867 KMP

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

  8. HDU 1867 A + B for you again 字符匹配

    解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...

  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. 【新词发现】基于SNS的文本数据挖掘、短语挖掘

    互联网时代的社会语言学:基于SNS的文本数据挖掘 python实现 https://github.com/jtyoui/Jtyoui/tree/master/jtyoui/word  这是一个无监督训 ...

  2. mac安装需要的骚操作

    显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles -bool true; KillAll Finder 允许任何来源 sudo spct ...

  3. 8月清北学堂培训 Day3

    今天是赵和旭老师的讲授~ 状态压缩 dp 状态压缩是设计 dp 状态的一种方式. 当普通的 dp 状态维数很多(或者说维数与输入数据有关),但每一维总量很少时,可以将多维状态压缩为一维来记录. 这种题 ...

  4. cesium billboard跨域问题2

    这篇主要是对上一篇博客cesium billboard出现跨域的原理分析 https://www.cnblogs.com/SmilingEye/p/11363837.html 1.源码位置 从Bill ...

  5. Golang的文件处理方式-常见的读写

    在 Golang 语言中,文件使用指向 os.File 类型的指针来表示的,也叫做文件句柄.注意,标准输入 os.Stdin 和标准输出 os.Stdout ,他们的类型都是 *os.File 哟.在 ...

  6. mysql 优化知识点

    附录: https://www.nowcoder.com/discuss/150059?type=0&order=0&pos=13&page=0 本文概要 概述 为什么要优化 ...

  7. Default Keyboard Shortcut Schemes

    Default Keyboard Shortcut Schemes All ReSharper actions can be invoked with keyboard shortcuts. Most ...

  8. Wamp win10 1077error

    检查日志发现了1077错误 State of services:   The service 'wampapache64' is NOT started EXIT error code:1077 He ...

  9. XM概述

    概述: Extensible Markup Language: 可扩展的标记语言 特点: 语法很严格 标签自定义 作用: * 存储数据 * 做配置文件 * 用于进行数据传输 文档声明: 标示这个文档是 ...

  10. C# mongodb 类库

    https://github.com/mongodb/mongo-csharp-driver/downloads https://github.com/mongodb/mongo-csharp-dri ...