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
 
Author
Wang Ye
 
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath> using namespace std; char s1[],s2[],s3[],s4[];
int nextt[];
int match[]; int main()
{
int i,j,k;
while(scanf("%s%s",s1+,s2+)!=EOF)
{
int l1 = strlen(s1+),l2 = strlen(s2+);
nextt[] = ;
for(i = ;i<=l2;i++)
{
int t = nextt[i-];
while(t&&s2[i]!=s2[t+]) t = nextt[t];
if(s2[i] == s2[t+]) t++;
nextt[i] = t;
}
match[] = ;
for(i = ;i<=l1;i++)
{
int t = match[i-];
while(t&&s1[i]!=s2[t+]) t = nextt[t];
if(s1[i] == s2[t+]) t++;
match[i] = t;
}
int t1 = match[l1];
for(i = ;i<=l1;i++)
s3[i] = s1[i];
for(i = l1+;i<=l1+l2-t1+;i++)
s3[i] = s2[i-l1+t1];
nextt[] = ;
for(i = ;i<=l1;i++)
{
int t = nextt[i-];
while(t&&s1[i]!=s1[t+]) t = nextt[t];
if(s1[i] == s1[t+]) t++;
nextt[i] = t;
}
match[] = ;
for(i = ;i<=l2;i++)
{
int t = match[i-];
while(t&&s2[i]!=s1[t+]) t = nextt[t];
if(s2[i] == s1[t+]) t++;
match[i] = t;
}
int t2 = match[l2];
for(i = ;i<=l2;i++)
s4[i] = s2[i];
for(i = l2+;i<=l2+l1-t2+;i++)
s4[i] = s1[i-l2+t2];
if(t1>t2) printf("%s",s3+);
else if(t2>t1) printf("%s",s4+);
else
{
int l = l1+l2-t2;
bool bb = ;
for(i = ;i<=l;i++)
{
if(s3[i]<s4[i])
{
printf("%s",s3+);
bb = ;
break;
}
else if(s3[i]>s4[i])
{
printf("%s",s4+);
bb = ;
break;
}
}
if(!bb) printf("%s",s3+);
}
puts("");
}
return ;
}

hdu1867A + B for you again的更多相关文章

随机推荐

  1. 《JavaScript 闯关记》之数组

    数组是值的有序集合.每个值叫做一个元素,而每个元素在数组中有一个位置,以数字表示,称为索引. JavaScript 数组是无类型的,数组元素可以是任意类型,并且同一个数组中的不同元素也可能有不同的类型 ...

  2. Javascript基础学习笔记

    什么是变量?变量是用于存储信息的容器变量的声明语法: var 变量名 变量名 = 值;变量要先声明再赋值变量可以重复赋值变量的命名规则变量必须以字母开头:变量也能以$和_符号开头(不过我们不推荐这么做 ...

  3. javascirpt的apply和call

    javascirpt的apply和call用法如下: var arr1=new Array("1","2","3");      var a ...

  4. jQuery日期和时间插件(jquery-ui-timepicker-addon.js)中文破解版使用

    <html> <head> <title></title> <link type="text/css" href=" ...

  5. HDU 4798 - Skycity

    告诉你一幢楼的高度,楼的层数,每层一样高. 每一层的底边是一个圆,下一层的玻璃一定要包括进上一层的底边. 每层玻璃铺成棱柱形,玻璃有最小面积限制. 问你这层楼最小的总玻璃数是多少. 求出每层最小的玻璃 ...

  6. mysql+php+pdo批量添加大数据

    1.使用insert into插入 ini_set('max_execution_time','0');//限制超时时间,因为第一种时间较长,索性设为0不限制 $pdo = new PDO(" ...

  7. mybatis使用order by注意

    直接用动态参数生成,不会排序: <if test="orderColumn!=null and orderColumn != ''"> ORDER BY #{order ...

  8. sublime工具 插件自动补全方法

    自动补全(emmet),输入对应的关键字(html标签)---tab键 http://www.emmet.io/ 代码片段 只需要输入自己的关键字--tab键 操作: 添加代码片段,然后保存 保存 使 ...

  9. U盘开发之安全U盘

    普通型安全U盘,虚拟KEY和U盘两个设备,由主机软件分别对KEY和U盘进行操作,U盘与上位机采用usb mass storage接口,KEY采用HID接口,两者均无需驱动.也有虚拟成光盘和U盘两个设备 ...

  10. 厂商自定义USB设备类概述

    USB协会将常用具有相同/相似功能的设备归为一类,并制定了相关的设备类规范,这样就能保障只要依照同样的规范标准,即使不同的厂商开发的USB设备也可以使用同样的驱动程序,而且操作系统中无须为每种设备提供 ...