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. JAVA RPC 生产级高可用RPC框架使用分享

    先放出链接,喜欢的给个star:https://gitee.com/a1234567891/koalas-rpc 一:项目介绍 koalas-RPC 个人作品,提供大家交流学习,有意见请私信,欢迎拍砖 ...

  2. IE与其他浏览器兼容性问题总结

    1.eval(idName) [问题描述]:IE.safari.Chrome浏览器下都可以使用eval(idName)或getElementById(idName)来取得id为idName的HTML对 ...

  3. 导出和导入eclipse中通过help安装的插件的地址

    这种方式和在线安装一样,唯一方便的就是不用再去翻找软件下载地址 导出已安装的插件: 打开Window ——>Preferences ——>Install/Update——>Avail ...

  4. 如何使用纯js实现一个带有灰色半透明背景的弹出框

    原文如何使用纯js实现一个带有灰色半透明背景的弹出框 // 加入透明背景 var body = document.body;var backgroundDiv = document.createEle ...

  5. /proc/sys/kernel/sysrq /proc/sysrq-trigger----强制重启/触发器

    LINUX远程强制重启/proc/sys/kernel/sysrq /proc/sysrq-trigger----触发器 ttp://blog.csdn.net/beckdon/article/det ...

  6. docker下搭建owncloud

    在ubuntu下 搭建owncloud 用docker-compose启动,owncloud.yml文件内容 owncloud: image: owncloud: restart: always 开机 ...

  7. mySQL 插入,更新和删除数据

    插入数据: 语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如 ...

  8. kubernetes监控(12)

    一.Weave Scope 1. weave scope 容器地图 创建 Kubernetes 集群并部署容器化应用只是第一步.一旦集群运行起来,我们需要确保一起正常,所有必要组件就位并各司其职,有足 ...

  9. shell练习题集合

    1. 获取ip或MAC地址(方法不唯一) [root@cicd ~]# ip a| grep 'inet' | awk -F " +" '{print $3}'| awk -F & ...

  10. delphi type

    声明: 1. type Name = Existing type; 2. type Name = type Existing type; 3. type Name = (EnumValue1 [=va ...