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. SIM300命令参考

    开机命令   AT+CFUN=1,1          //此命令可以开启simcom模块的大部分功能,一般在初始化模块的时候都要写上: AT&F                        ...

  2. Django(七)

    一.ModelForm操作及验证 1.class Meta:class Meta: #注意以下字段不能加逗号 model = models.UserInfo #这里的all代指所用的字段,也可以是一个 ...

  3. TortoiseGit生成PuttyKey与GitHub的SSH进行关联

    1.打开Puttygen 要到进度条满格为止,知道出现如下界面: 把上面的Key复制. 最后点击[Save private key]保存. 2.登录GitHub进行如下操作: Settings-> ...

  4. javaScript面向对象继承方法经典实现

    转自原文javaScript面向对象继承方法经典实现 JavaScript的出现已经将近20多年了,但是对这个预言的褒贬还是众说纷纭.很多人都说JavaScript不能算是面向对象的变成语言.但是Ja ...

  5. WebApplicationContext初始化(转)

    ApplicationContext是Spring的核心,Context我们通常解释为上下文环境,我想用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”了:在We ...

  6. PHP之实现双向链表(代码篇)

    <?php/** * PHP之实现双向链表 */class Hero{ public $pre=null; public $no; public $name; public $next=null ...

  7. 求int型数据在内存中存储时1的个数

    1.求int型数据在内存中存储时1的个数 输入一个int型数据,计算出该int型数据在内存中存储时1的个数. 我们非常easy想到例如以下方法: #include <iostream> u ...

  8. 复制DropDownList

    DropDownList ddlA; ListItem[] ar = new ListItem[ddlB.Items.Count]; ddlB.Items.CopyTo(ar,0); ddlA.Dat ...

  9. 当使用Spring MVC @Valid对输入框进行验证的时候,可能会遇到以下的异常:Neither BindingResult nor plain target object for bean name ‘mybean’ available as request attribute

    转自:https://www.cnblogs.com/wenhulu/p/5555457.html 当使用Spring MVC @Valid对输入框进行验证的时候,可能会遇到以下的异常: Neithe ...

  10. hihoCoder-1633 ACM-ICPC北京赛区2017 G.Liaoning Ship’s Voyage 线段与三角形规范相交

    题面 题意:给你一个20*20的地图,起点(0,0),终点(n-1,n-1),有障碍的点为‘#’,每次可以向8个方向走一步,还给了一个三角形,除了障碍以外,到这8个方向上的点的线段如果没有与三角形相交 ...