Hdu传送门
连接s1,s2, kmp求前缀和后缀(给了两个字符串,有时可以连接解题 )
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define ms(i,j) memset(i, j, sizeof i);
const int MAXN = 50000 + 5;
char s1[MAXN*3], s2[MAXN];
int n1, n2, n, f[MAXN*3];
void init()
{
n1 = strlen(s1), n2 = strlen(s2);
n = n1 + n2;
for (int i=n1;i<n;i++)
{
s1[i] = s2[i-n1];
}
}
void solve()
{
f[0] = f[1] = 0;
for (int i=1;i<n;i++)
{
int j = f[i];
while (j && s1[i]!=s1[j]) j = f[j];
f[i+1] = (s1[i]==s1[j]) ? (j+1) : (0);
}
int i = n;
while (f[i])
{
if (f[i]<=min(n1,n2))
{
for (int j=0;j<f[i];j++) putchar(s1[j]);
printf(" %d\n", f[i]);
return ;
}
i = f[i];
}
printf("%d\n", 0);
}
int main()
{
while (scanf("%s%s", s1, s2)==2)
{
init();
solve();
}
return 0;
}