#include <stdio.h>
#include <string.h>
#define MAXN 100005
int next[MAXN];
char str1[MAXN],str2[MAXN];
void getNext(char *str)
{
int len=strlen(str);
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if (j==-1||str[i]==str[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
int KMP(char *s,char *t)
{
int len1,len2,i,j;
i=j=0;
len1=strlen(s);
len2=strlen(t);
getNext(t);
while(i<len1&&j<len2)
{
if(j==-1||s[i]==t[j])
{
i++;
j++;
}
else
j=next[j];
}
if(i==len1)
return j;
return 0;
}
int main()
{
int x,y;
while(scanf("%s%s",str1,str2)!=EOF)
{
x=KMP(str1,str2);
y=KMP(str2,str1);
if(x==y)
{
if(strcmp(str1,str2)<0)
printf("%s%s\n",str1,str2+x);
else
printf("%s%s\n",str2,str1+x);
}
else if(x>y)
printf("%s%s\n",str1,str2+x);
else
printf("%s%s\n",str2,str1+y);
}
return 0;
}
hdu1867 A + B for you again
最新推荐文章于 2017-09-20 20:26:58 发布