[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher J (kmp扩展)

本文介绍了如何使用KMP算法及其扩展版解决寻找字符串s1的最长前缀,该前缀同时也是字符串s2的后缀的问题。通过求解s2的每个后缀与s1的公共前缀,可以利用KMP算法进行高效查找。
摘要由CSDN通过智能技术生成

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值