您的位置:首页 > 其它

HDU_2594

2015-07-22 09:46 417 查看
//题意很简单,就是给定两个字符串,在第一个字符串中找到一个最大前缀作为第二个字符串的后缀.

AC代码:

#include<stdio.h>
#include<string.h>
#define N 50005
char t[N];
char s[N];
int next[N];
int len1;
int len2;
void get_next()
{
int i=0;
int j=-1;
next[0]=-1;
while(i<len2)
{
if(j==-1||s[i]==s[j])
{
i++;
j++;
next[i]=j;
}
else
{
j=next[j];
}
}
}
void kmp()
{
int i=0;
int j=0;
while(i<len1)
{
if(j==-1||t[i]==s[j])
{
i++;
j++;
}
else
{
j=next[j];
}
}
if(!j)
{
printf("%d",j);
}
else
{
for(int k=0;k<j;k++)
{
printf("%c",s[k]);
}
printf(" %d",j);
}
printf("\n");
}
int main()
{
while(scanf("%s%s",s,t)!=EOF)
{
len1=strlen(t);
len2=strlen(s);
get_next();
kmp();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: