KMP应用http://acm.hdu.edu.cn/showproblem.php?pid=2594

riemann与marjorie拼接后riemannmarjorie前缀与后缀公共部分为 rie 长度为 3(即next[l] = next[14]的值,l为拼接后的长度)
但:aaaa与aa拼接后aaaaaa,next[l]不是结果,所以在aaaa和aa间加“#”,即aaaa#aa;
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#define N 50010 char s1[ * N], s2[N];
int next[ * N]; void GetNext(char s[])
{
int k = -, j = , l = strlen(s);
next[] = -;
while(j < l)
{
if(k == - || s[j] == s[k])
{
k++;
j++;
next[j] = k;
}
else
k = next[k];
}
} int main()
{
int l1, l2, i, l;
while(scanf("%s%s", s1, s2) != EOF)
{
l1 = strlen(s1);
l2 = strlen(s2);
s1[l1] = '#';
s1[l1 + ] = '\0';
strcat(s1, s2);
GetNext(s1);
l = l1 + l2; if(next[l + ] == )
printf("0\n");
else
{
for(i = ; i < next[l + ] ; i++)
printf("%c", s1[i]);
printf(" %d\n", next[l + ]);
}
}
return ;
}
上一篇:【Alpha】Scrum Meeting 8


下一篇:[转]在 Windows Server 2012 上安装 IIS 8