题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1867
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
typedef __int64 LL;
const int N=100005;
const LL II=1000000007;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
char s1[N],s2[N];
int len,next[2*N];
void getnext(char *p,int wlen)
{
int j=0,k=-1;
next[0]=-1;
while(j<wlen)
{
if(k==-1||p[j]==p[k])
{
j++; k++;
next[j]=k;
}
else
k=next[k];
}
}
int kmp(char *text,char *word)
{
int i=0,j=0,tlen=strlen(text);
while(i<tlen)
{
if(j==-1||text[i]==word[j])
j++,i++;
else
j=next[j];
}
return j;
}
int main()
{
int ca=0,i,j,T;
while(scanf("%s%s",s1,s2)!=EOF)
{
int len1,len2,t1,t2;
len1=strlen(s1);
len2=strlen(s2);
getnext(s2,len2);
t1=kmp(s1,s2);//s2模式串
getnext(s1,len1);
t2=kmp(s2,s1);//s1模式串
if(t1==t2)
{
if(strcmp(s1,s2)<0)
printf("%s%s\n",s1,s2+t1);
else
printf("%s%s\n",s2,s1+t1);
continue;
}
if(t1<t2)
printf("%s%s\n",s2,s1+t2);
else
printf("%s%s\n",s1,s2+t1);
}
return 0;
}