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. 【TFS】增加组员,以及用户权限分配

    一.创建windows用户. 二.TFS ->组成员资格->双击 项目集合管理员->添加创建的Windows用户(最高权限) 三.设置权限: TFS权限的复杂,其实也不是很复杂,它只 ...

  2. 关于ASP.NET 中站点地图sitemap 的使用

    在ASP.NET  MVC 如此火热的时期,我竟然不适时宜的谈起ASP.NET ,恐怕会引来一阵嘲笑.最为无趣的是,讲解的竟然还是其中的一个控件.oh~~  my god!my out! ^_^ Si ...

  3. Android中webView的基础使用(一)

    WebView是View的一个子类,可以让你在activity中显示网页. 可以在布局文件中写入WebView:比如下面这个写了一个填满整个屏幕的WebView: <?xml version=& ...

  4. 什么时候会刷新备库控制文件refresh the standby database control file?

    通过合理的设置,对于Primary的绝大数操作,都是可以传递到Physical Standby,datafile的操作是通过STANDBY_FILE_MANAGEMENT参数来控制的,但是即使STAN ...

  5. Mplayer ARM平台下交叉编译

    下载MPlayer http://www.mplayerhq.hu/design7/dload.html 编译环境 系统 : ubuntu 11.04 交叉编译器版本 : Sourcery G++ L ...

  6. P - A + B(第二季水)

    Description          读入两个小于100的正整数A和B,计算A+B.                 需要注意的是:A和B的每一位数字由对应的英文单词给出.             ...

  7. VS C4819 编译错误解决方法

    偶尔用别人的代码,出现: warning C4819: The file contains a character that cannot be represented ). Save the fil ...

  8. Floyd算法(弗洛伊德算法)

    算法描述: Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法.从图的带权邻接矩阵A=[a(i,j)] n×n开始,递归地进行n次更新,即由矩阵D(0)=A,按 ...

  9. LogBoy运行截图

  10. Number Steps

    Number Steps Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...