A + B for you again
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8938 Accepted Submission(s): 2166
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 2008杭电集训队选拔赛——热身赛
Recommend lcy | We have carefully selected several similar problems for you: 1711 1866 1277 1865 1869
#include<iostream>
#include<queue>
#include<stdio.h>
#include<string>
#include<cmath>
using namespace std;
typedef long long LL;
#define maxn (100001*2)
int Next[maxn];
/*
题意很简单,
几个点就是如何判断字典序,
还有就是截取的问题,,,
这题该打::;我竟然忽略了最大前缀后缀可以覆盖其中一个串的情况,
处理方法是在中间添加一个分割符,,就可以了
*/
void getNext(string t)
{
int k=-1,sz=t.size();
Next[0]=-1;
for(int i=1;i<sz;i++)
{
while(k>-1&&t[k+1]!=t[i])
k=Next[k];
if(t[k+1]==t[i])
k++;
Next[i]=k;//i-k
}
}
int main()
{
ios::sync_with_stdio(false);
string s1,s2;
while(cin>>s1>>s2)
{
string tmp1=s2+"#"+s1,tmp2=s1+"#"+s2;
int len=tmp1.size();
getNext(tmp1);
int re1=Next[len-1]+1;
getNext(tmp2);
int re2=Next[len-1]+1;
tmp1=s1+s2.substr(re1,s2.size()-re1);
tmp2=s2+s1.substr(re2,s1.size()-re2);
if(re1>re2) cout<<tmp1;
else if(re2>re1) cout<<tmp2;
else
{
if(tmp1<tmp2) cout<<tmp1;
else cout<<tmp2;
}
cout<<endl;
}
return 0;
}