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 <stdio.h>
#include <string.h> int next[100005]; void getnext(char str[])
{
int i = 1,j = 0;
int len = strlen(str);
next [0] = -1;
while(i < len)
{
if(j == -1 || str[i] == str[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
} int kmp(char str1[],char str2[])
{
int i= 0,j = 0;
int len1 = strlen(str1),len2 = strlen(str2);
getnext(str2);
while(i<len1 && j<len2)
{
if(j == -1 || str1[i] == str2[j])
{
i++;
j++;
}
else
j = next[j];
}
if(i == len1)
return j;
return 0;
} int main()
{
int x,y;
char str1[100005],str2[100005];
while(scanf("%s%s",str1,str2)!=EOF)
{
x = kmp(str1,str2);
y = kmp(str2,str1);
if(x == y)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+x);
}
else
{
printf("%s",str1);
printf("%s\n",str2+x);
}
}
else if(x>y)
{
printf("%s",str1);
printf("%s\n",str2+x);
}
else
{
printf("%s",str2);
printf("%s\n",str1+y);
}
} return 0;
}

HDU1867 - A + B for you again的更多相关文章

  1. hdu1867之KMP

    A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

  3. KMP回顾学习

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

随机推荐

  1. JAVA的输入输出基本操作样例

    这些类的继承关系有些类似,弄一个作为样例,理解一下其中的机制. package cc.openhome; import java.io.*; public class Member { private ...

  2. C#编程中,在页面上如何弹出确认删除对话框

    对于页面完成一个操作后,弹出一个对话框提示是否“操作成功”.举例如下:Response.Write("<script>alert('删除成功!')</script>& ...

  3. spring5.0新特性

    spring5.0新特性 学习了:http://blog.csdn.net/u012562943/article/details/77449666 https://www.cnblogs.com/xu ...

  4. C语言可变參实现參数累加返回

    C语言可变參的作用真的是很大,自从发表了可变參怎样实现printf.fprintf,sprintf的文章以来.便有不少博友私信问我实现的机制,我也解释了相关的知识点.今天,我们借着这个机会,再来举一个 ...

  5. csu 1030: 素数槽

     素数槽 Description 处于相邻的两个素数p和p + n之间的n - 1个连续的合数所组成的序列我们将其称为长度为n的素数槽.比如,‹24, 25, 26, 27, 28›是处于素数23 ...

  6. C语言读取文件大量数据到数组

    针对.txt文档的大量有规律数据,譬如100行8列的数据将其读取到二维数组(矩阵)中,留作之后的数据处理. 改程序通过宏定义的方法来确定将要读取程序的行数和列数,将数据读取到二维数组data[100] ...

  7. hdu 1002 A + B Problem II(大正整数相加)

    代码: #include<cstdio> #include<cstring> #define Min(a,b) ((a)<(b)?(a):(b)) using names ...

  8. IOS UITextView光标位置在中间的问题

    在viewDidLoad中 if ([selfrespondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) { se ...

  9. Cache操作类

    封装类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sys ...

  10. nyoj--284--坦克大战(bfs模板)

    坦克大战 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Many of us had played the game "Battle city" i ...