Link:https://vjudge.net/contest/70325#problem/J
题意:Write a program that, when given strings s1 and s2, finds the longest prefix of s1 that is a suffix of s2.(求s1的最长前缀 同时是s2的后缀.)
思路:求s2的每个后缀与s1的公共前缀,kmp扩展.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const maxn=50000+5;
char str[maxn];
char mode[maxn];
int nextt[maxn],extend[maxn];
void getNext(char mode[],int nextt[],int modeLen)
{
int i,a,p;
a=p=0,nextt[0]=modeLen;
for(i=1;i<modeLen;i++)
{
if(i>=p || i+nextt[i-a]>=p)
{
if(i>=p)p=i;
while(p<modeLen && mode[p]==mode[p-i]) ++p;
nextt[i]=p-i;
a=i;
}
else nextt[i]