Manacher最长回文子串
Posted thusloop
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Manacher最长回文子串相关的知识,希望对你有一定的参考价值。
//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
char A[maxn* 2],a[maxn];
int B[maxn * 2];
int manacher(char s[], int len)
int l = 0;
A[l++] = '$'; //0下标存储为其他字符
A[l++] = '#';
for (int i = 0; i < len; i++)
A[l++] = s[i];
A[l++] = '#';
A[l] = 0; //空字符
int mx = 0;
int id = 0;
for (int i = 1; i < l; i++)
B[i] = mx > i ? min(B[2 * id - i], mx - i) : 1;
while (A[i + B[i]] == A[i - B[i]])
B[i]++;
if (i + B[i] > mx)
mx = i + B[i];
id = i;
int ans = 0;
for (int i = 0; i < 2 * len + 2; i++)
ans = max(ans, B[i] - 1);
return ans;
signed main()
cin>>a;
int n=strlen(a);
cout<<manacher(a,n)<<"\\n";
/*
* abaaba
* i: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
* A: $ # a # b # a # a # b # a # '\\0'
* B: 1 1 2 1 4 1 2 7 2 1 4 1 2 1 //以第i个为中心的回文半径(包括第i个)
*/
以上是关于Manacher最长回文子串的主要内容,如果未能解决你的问题,请参考以下文章