洛谷 P3805 【模板】manacher

2023-12-25 21:40:13

【模板】manacher

题目描述

给出一个只由小写英文字符 a , b , c , … y , z \texttt a,\texttt b,\texttt c,\ldots\texttt y,\texttt z a,b,c,y,z 组成的字符串 S S S ,求 S S S 中最长回文串的长度 。

字符串长度为 n n n

输入格式

一行小写英文字符 a , b , c , ? ? , y , z \texttt a,\texttt b,\texttt c,\cdots,\texttt y,\texttt z a,b,c,?,y,z 组成的字符串 S S S

输出格式

一个整数表示答案。

样例 #1

样例输入 #1

aaa

样例输出 #1

3

提示

1 ≤ n ≤ 1.1 × 1 0 7 1\le n\le 1.1\times 10^7 1n1.1×107

AC代码

#include<bits/stdc++.h>
using namespace std;
string s,oo;
int r,p[110000010],mid,ans;
void sol(string oo){
	s+='~';
	s+='*';
	for(int i=0;i<oo.size();i++){
		s+=oo[i];
		s+='*';
	}
}
int main(){
	cin>>oo;
	sol(oo);
	for(int i=0;i<s.size();i++){
		if(r>i) {
			p[i]=min(r-i,p[2*mid-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]);
	}
	cout<<ans-1;
	
	return 0;
} 

文章来源:https://blog.csdn.net/m0_61360607/article/details/135208920
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。