跳到正文

个人记录

24818海亮T2代码

IceBee约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前

先对原字符串进行manacher,并求出原字符串每个前缀子串必定字典序小于它的长度相等字符串的数量,然后枚举回文串长度,判断 当 回文串前半段 恰好和 原前缀子串 相等时,产生的回文串是否符合字典序小于等于原字符串的要求

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5000015;
const long long M =998244353;
long long ans;
long long n,bn;
int p[2*N],pa[N];
long long res[N];
char a[N],b[2*N];
int main(){
	freopen("string.in","r",stdin);
	freopen("string.out","w",stdout);
	cin>>a+1;
	n=strlen(a+1);
	bn=2*n+2;
	a[0] = '&';
	for(int i=0;i<=n;i++){
		b[2*i] = a[i];
		b[2*i+1] = '|';
	}
	b[bn] ='!';
	int mid=0,r=0;
	for(int i=0;i<=bn;i++){
		int i1=mid*2-i;
		int rmx=min(i+p[i1],r);
		int l=rmx-i;
		if(rmx == r){
			while(b[i+l+1] == b[i-l-1] && i+l+1<=bn)  l++;
		}
		p[i] =l;
		if(i+l > r){
			r=i+l;
			mid=i;
		}
	}

	for(int i=1;i<=n/2+1;i++){
		res[i] =( ((res[i-1])*26)%M + (a[i]-'a')%M )%M;
	}
	for(int i=1;i<=n;i++){
		int ans1=ans;
		if(i%2==0){
			ans=ans+res[i/2];
			if(p[i+1] >= i)  ans++;
			else if(b[i+2+p[i+1]] > b[i-p[i+1]]) ans++;
		}
		else if(i%2==1){
			ans=ans+res[i/2+1];
			if(p[i+1] >= i)  ans++;
			else if(b[i+2+p[i+1]] > b[i-p[i+1]]) ans++;
		}
		ans=ans%M;
	}
	cout<<ans;
	return 0;
}
原文