hdu1867A + B for you again

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 ;
}
上一篇:CentOS yum安装和配置MySQL(转载)


下一篇:javaSE_05Java中方法(函数)与重载、递归