#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int M =111000;
char s1[M],s2[M];
int l1,l2;
int fail[M];
// 题意: 求与s2后缀相等的s1的最长前缀
// 连接接s1,s2 求出 next[l1+l2]中不大于min(l1,l2)的前缀即可
void Get()
{
int i=0,k=-1;
fail[0]=-1;
while(i<l1+l2)
{
if(k==-1||s1[i]==s1[k])
{
fail[i+1]=k+1;
i++;
k++;
}
else
{
k=fail[k];
}
}
}
int main()
{
while(scanf("%s",s1)!=EOF)
{
scanf("%s",s2);
l1=strlen(s1);
l2=strlen(s2);
strcat(s1,s2);
Get();
int len=l1+l2;
while(fail[len]>l1 || fail[len]>l2) //相同的一段不能越串
len=fail[len];
len=fail[len]; //找到合法的len
for(int i=0;i<len;i++)
{
cout<<s1[i];
}
if(len)
cout<<" ";
cout<<len<<endl;
}
return 0;
}
hdu 2594 next数组简单运用
最新推荐文章于 2019-09-27 20:36:43 发布
2715

被折叠的 条评论
为什么被折叠?



