【模板】Manacher

题目跳转

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1.1e7+5;
string s,tmp;
ll n,r,mid,p[maxn<<1],ans;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>tmp;
	s="@#";
	for(ll i=0;i<tmp.size();i++){
		s+=tmp[i];
		s+='#';
	}
	s+='$';
	n=s.size();
	for(ll i=0;i<n;i++){
		if(i<r){
			p[i]=min(p[(mid<<1)-i],r-i);
		}else{
			p[i]=1;
		}
		while(s[i+p[i]]==s[i-p[i]]){
			p[i]++;
		}
		if(i+p[i]>r){
			r=i+p[i];
			mid=i;
		}
		ans=max(ans,p[i]-1);
	}
	cout<<ans;
	return 0;
}
posted @ 2026-11-09 11:18  beiciqi  阅读(6)  评论(0)    收藏  举报