浅谈 Manacher
前言:
关于 Manacher,有很多说法。有人说是马拉车,有人说是 Man 拉车。不多说了,本期难度一般,现在开讲。
何为 Manacher:
Manacher 算法主要是处理字符串中关于回文串的问题的,它可以在
怎么用:
我们分成三步:\
第一步,在原字符串中插入井号,如下:\
\
第二步,对于新的字符串,我们进行求
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,str,w;
int n,m,p[110000005];
int k;
const int mod=998244353;
void add(){
str=" ";
int len=s.size();
for(int i=0;i<s.size();i++){
str+="#";
str+=s[i];
}
str+="#";
str+="@";
m=len*2+1;
}
void manacher(){
int r=0;
int mid=0;
int ans=0;
for(int i=1;i<=m;i++){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while(str[i+p[i]]==str[i-p[i]]){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
ans=max(ans,p[i]-1);
}
cout<<ans;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
cin>>s;
add();
manacher();
return 0;
}
例题:
本期题目是 5 蓝 1 紫,实际上是 4 蓝 2 紫,值得好好练习。
P3805 【模板】manacher:
模板题,直接写。
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,str,w;
int n,m,p[110000005];
int k;
const int mod=998244353;
void add(){
str=" ";
int len=s.size();
for(int i=0;i<s.size();i++){
str+="#";
str+=s[i];
}
str+="#";
str+="@";
m=len*2+1;
}
void manacher(){
int r=0;
int mid=0;
int ans=0;
for(int i=1;i<=m;i++){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while(str[i+p[i]]==str[i-p[i]]){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
ans=max(ans,p[i]-1);
}
cout<<ans;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
cin>>s;
add();
manacher();
return 0;
}
P3501 [POI 2010] ANT-Antisymmetry:
相对于模板题的判断部分有所改变,然后回文串的数量为
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,str;
int n,m,p[11000005];
void add(){
str=" "+str;
int len=s.size();
for(int i=0;i<s.size();i++){
str+="#";
str+=s[i];
}
str+="#";
str+="@";
m=len*2+1;
}
void manacher(){
int r=0;
int mid=0;
int ans=0;
for(int i=1;i<=m;i+=2){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while((str[i+p[i]]+str[i-p[i]]=='1'+'0'||str[i+p[i]]+str[i-p[i]]=='#'+'#')){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
ans+=p[i]/2;
}
cout<<ans;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
cin>>n;
cin>>s;
add();
manacher();
return 0;
}
P1659 [国家集训队] 拉拉队排练:
这道题其实就是把所有的回文串的长度求出来,然后从大的开始逐步乘起来,假设有一个
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,str;
int n,m,p[3000005],k;
int cnt[3000005];
const int MOD=19930726;
int poow(int a,int b){
int cul=1;
while(b){
if(b%2) cul=cul*a%MOD;
a=a*a;
a%=MOD;
b/=2;
}
return cul;
}
void add(){
str=" "+str;
int len=s.size();
for(int i=0;i<s.size();i++){
str+="#";
str+=s[i];
}
str+="#";
str+="@";
m=len*2+1;
}
void manacher(){
int r=0;
int mid=0;
int ans=1;
for(int i=1;i<=m;i++){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while(str[i+p[i]]==str[i-p[i]]){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
cnt[p[i]-1]++;
}
for(int i=n;i>=1;i--){
if(i%2==0) continue;
if(k>=cnt[i]){
ans*=poow(i,cnt[i]);
ans%=MOD;
k-=cnt[i];
}
else{
if(k>0) ans*=poow(i,k);
ans%=MOD;
k=0;
break;
}
cnt[i-2]+=cnt[i];
}
if(k!=0) cout<<-1;
else cout<<ans;
}
signed main(){
// freopen("y.in","r",stdin);
cin>>n>>k;
cin>>s;
add();
manacher();
return 0;
}
P4555 [国家集训队] 最长双回文串&P4287 [SHOI2011] 双倍回文:
这两道题放一起讲,其实就是对于一个回文串的两侧的中心点为中心的回文串长度,大于等于这一侧的长度。
P4555 [国家集训队] 最长双回文串:
#include<bits/stdc++.h>
using namespace std;
string s,str;
int n,m,p[1000005],L[1000005],R[1000005];
int f[1000005],id[1000005];
void add(){
str=" "+str;
int len=s.size();
for(int i=0;i<s.size();i++){
str+="#";
str+=s[i];
}
str+="#";
str+="@";
m=len*2+1;
}
void manacher(){
memset(f,0x3f,sizeof(f));
for(int i=1;i<=1000000;i++) id[i]=0;
int r=0;
int mid=0;
int ans=0,last=0;
for(int i=1;i<=m;i++){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while(str[i+p[i]]==str[i-p[i]]){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
L[i+p[i]-1]=max(L[i+p[i]-1],p[i]-1);
R[i-p[i]+1]=max(R[i-p[i]+1],p[i]-1);
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
cin>>s;
add();
manacher();
for(int i=3;i<=m;i+=2) R[i]=max(R[i],R[i-2]-2);
for(int i=m-2;i>=1;i-=2) L[i]=max(L[i],L[i+2]-2);
int ans=0;
for(int i=1;i<=m;i+=2){
if(R[i]&&L[i]) ans=max(ans,L[i]+R[i]);
}
cout<<ans;
return 0;
}
P4287 [SHOI2011] 双倍回文:
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n,m;
string x,str;
void add(){
str="!";
for(int i=0;i<n;i++){
str+="#";
str+=x[i];
}
str+="#";
str+="@";
m=n*2+1;
}
int p[50000005];
void manacher(){
int r=0,mid=0;
int ans=0;
for(int i=1;i<=m;i++){
p[i]=(i<r?min(p[mid*2-i],r-i+1):1);
while(str[i-p[i]]==str[i+p[i]]){
p[i]++;
if(str[i]=='#'&&(p[i]-1)%4==0&&p[i-p[i]/2]>p[i]/2){
ans=max(ans,p[i]-1);
}
}
if(r<i+p[i]){
r=i+p[i]-1;
mid=i;
}
}
cout<<ans<<"\n";
}
signed main(){
cin>>n>>x;
add();
manacher();
return 0;
}
/*
!#a#a#a#a#@
12345678901
*/
P11749 「TPOI-1C」Standard Problem.:
思路:
这题其实是道假题,我们可以发现如果字符串本身不是一个回文串,那么答案一定是
算后好对于答案取模输出即可。
code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
string s,str,w;
int n,m,p[50000005];
int k;
const int mod=998244353;
void add(){
str=" ";
int len=w.size();
for(int i=0;i<w.size();i++){
str+="#";
str+=w[i];
}
str+="#";
str+="@";
m=len*2+1;
}
int manacher(){
// memset(p,0,sizeof(p));
int r=0;
int mid=0;
int ans=0,last=0;
for(int i=1;i<=m;i++){
p[i]=((i<r)?min(p[2*mid-i],r-i):1);
while(str[i+p[i]]==str[i-p[i]]){
p[i]++;
}
if(i+p[i]>r){
r=p[i]+i;
mid=i;
}
ans+=p[i]/2;
ans%=mod;
}
return ans;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cout.tie(0)->sync_with_stdio(0);
int T;
cin>>T;
while(T--){
cin>>n>>k;
cin>>s;
w=s;
add();
int A=manacher();
w=s+s;
add();
int B=manacher();
w=s+s+s;
add();
int C=manacher();
cout<<((((B-A+mod)%mod)*((k-1+mod)%mod))%mod+(C-2*B+A+mod*2)%mod*(((k-1)*(k-2)/2)%mod)+A)%mod<<"\n";
}
return 0;
}
/*
abaaba
*/
后话:
想要我更新更多算法的人回复 Clyyds 加上算法名,最后求大家点个赞吧。