//hdu 1867
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int MAX=100009;
char str1[MAX],str2[MAX];
int next_[MAX];
void getNext(char *pattern)
{
int i=0,j=-1;
next_[0]=-1;
int len=strlen(pattern);
while(i<len)
{
while(j!=-1 && pattern[i]!=pattern[j]) j=next_[j];
next_[++i]=++j;
}
}
int KMP(char *text,char *pattern)
{
int i=0,j=0;
int len1=strlen(text),len2=strlen(pattern);
getNext(pattern);
while(i<len1 && j<len2)
{
if(j==-1 || pattern[j]==text[i])
j++,i++;
else
j=next_[j];
}
if(i>=len1) return j;
return 0;
}
int main(void)
{
// freopen("c.txt","r",stdin);
while(~scanf("%s%s",str1,str2))
{
int x=KMP(str1,str2);
int y=KMP(str2,str1);
if(x==y)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+x);
}
else
{
printf("%s",str1);
printf("%s\n",str2+x);
}
}
else if(x>y)
{
printf("%s",str1);
printf("%s\n",str2+x);
}
else
{
printf("%s",str2);
printf("%s\n",str1+y);
}
}
return 0;
}
KMP裸题模板
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 第1章 第一个C程序第2章 C语言基础第3章 变量和数据类型第4章 顺序结构程序设计第5章 条件结构程序设计第6章...
- 周末午后,看到摊开在枕边的这本书却没有翻开的印象,拿起来才想起前几天侄女曾赖在房间里看书。在2016年这个高温肆虐...