跳到正文

题解

题解:P11749 「TPOI-1C」Standard Problem.

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

文章信息

发布
2025年7月22日
收录
2026年6月14日
更新
2026年10月31日
篇幅
约 2 分钟
获赞
1
题目
P11749

版本

  1. v1当前

思路:

这道题我们可以发现如果字符串本身不是一个回文串,那么答案一定是 00。我们知道这个后就可以发现,从连着三个起,不管是连几个,他们的答案都是一样的。也就是首尾两个字符串,中间加个回文串。
接着我们就能把公式算出来,设一个为 AA,两个为 BB,三及以上为 CC,可得:

(B−A)×(k−1)+(C−2×B+A)×(k−1)×(k−2)2+A(B-A)\times(k-1)+(C-2\times B+A)\times\dfrac{(k-1)\times(k-2)}{2}+A

算后好对于答案取模输出即可。

code:

C++
#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
*/
原文