【SP7586】NUMOFPAL - Number of Palindromes

题面

https://www.luogu.org/problem/SP7586

题解

#include<cstdio>
#include<iostream>
#include<cstring>
#define ri register int
#define N 11000050
#define uLL unsigned long long
using namespace std;
char s0[N];
char s1[N<<1];
int n;
int hw[N<<1],maxr,mid;
int main(){
  scanf("%s",s0+1);
  n=strlen(s0+1);
  for (ri i=1;i<=2*n+1;i++) if (i%2==1) s1[i]='#'; else s1[i]=s0[i/2];
  maxr=0; mid=0;
  for (ri i=1;i<=2*n+1;i++) {
    if (i<=maxr) hw[i]=min(hw[2*mid-i],maxr-i);
    while (i+hw[i]+1<=2*n+1 && i-hw[i]-1>=1 && s1[i+hw[i]+1]==s1[i-hw[i]-1]) hw[i]++;
    if (i+hw[i]>maxr) maxr=i+hw[i],mid=i;
  }
  uLL ans=0;
  for (ri i=1;i<=2*n+1;i++) if (s1[i]=='#') ans+=hw[i]/2; else ans+=hw[i]/2+1;
  cout<<ans<<endl;
}

猜你喜欢

转载自www.cnblogs.com/shxnb666/p/11279708.html
今日推荐