2023年ICPC亚洲合肥赛区赛 C. Cyclic Substrings

题目

题解

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
// #define int long long
#define ll long long
const int maxn = 6e6 + 5;
const int mod = 998244353;
int fail[maxn];//fail[i]表示i结点代表的回文串的最大回文后缀的编号 
int len[maxn]; //len[i]表示结点i代表的回文串的长度 
int trie[maxn][26], tot = 1;//tot初始为1!!!! 
int cnt[maxn];//结点i代表的回文串出现了多少次
int ind[maxn];
string s;
int get(int x, int i){//x是以s[i-1]结尾的回文串,返回以s[i-1]结尾且s[i-len[x]-1]==s[i]的回文串的对应结点编号 
	while(i - len[x] - 1 < 0 || s[i - len[x] - 1] != s[i]) x = fail[x];
	return x;
}
signed main(){
	int i, j;
	int n;
	cin >> n;
	cin >> s;
	s = s + s;
	int cur = 0;
	int pos, last = 0;
	fail[0] = 1;
	fail[1] = 0;
	len[0] = 0;
	len[1] = -1;
	for(i = 0; i < 2 * n; i++){
		pos = get(cur, i);//cur是以s[i-1]结尾的最大回文串 
		int idx = s[i] - '0';
		if(!trie[pos][idx]){ 
			tot++; 
			int t = get(fail[pos], i);
			fail[tot] = trie[t][idx];
			ind[trie[t][idx]]++;
			trie[pos][idx] = tot;//把新建的结点接在pos下面 
			len[tot] = len[pos] + 2;
			// cnt[tot] = cnt[fail[tot]] + 1;
		}
		cur = trie[pos][idx];
		if(i > n - 1)//只计算以 后半串的字符 结尾的最长回文串,否则会重复
			cnt[cur]++;//结点cur代表的回文串出现了多少次
	}
	
	ll res = 0;
	queue<int> q;
	for(int i = 2; i <= tot; i++){
		// if(len[i] > n) continue;
		if(!ind[i]){
			q.push(i);
		}
	}
	while(!q.empty()){
		int u = q.front();
		q.pop();
		int v = fail[u];
		// if(len[v] > n) continue;
		cnt[v] += cnt[u];//一个回文串出现一次,那么它的回文后缀也出现一次
		if(--ind[v] == 0){
			q.push(v);
		}
	}
	for(int i = 2; i <= tot; i++){
		if(len[i] > n) continue;
		// cout << i << ' ' << len[i] << ' ' << cnt[i] << '\n';
		res = (res + 1LL * len[i] * cnt[i] % mod * cnt[i] % mod) % mod;
	}
	cout << res << '\n';
	return 0;
}
相关推荐
jktantan1 小时前
注解式脱敏:用 Jackson 的 ContextualSerializer 做字段级策略(一)
spring boot·后端·算法·spring cloud·系统架构
Dream it possible!14 小时前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊14 小时前
算法之双指针系列(一)
算法·leetcode
十二14 小时前
【洛谷题解】P1083 [NOIP 2012 提高组] 借教室(二分答案 + 差分)
数据结构·c++·算法·蓝桥杯
小此方15 小时前
[算法实战论:动态规划]回文串问题的dp解法,回文串问题的母题,以及以此为基础的派生题
算法·动态规划
Pniubi16 小时前
力扣560.和为k的子数组
算法·leetcode·职场和发展
D e re k ZH16 小时前
C++ 从零入门(一):基础语法与程序结构
数据结构·c++·算法
Pniubi17 小时前
力扣438.找到字符串中所有字母异位词
算法·leetcode·职场和发展
東隅已逝,桑榆非晚19 小时前
算法6. 双指针法-查找总价格为目标的两个商品
c++·学习·算法
Eyfcom19 小时前
〖物联网终端实战 01〗UWB 室内高精度定位:仓储 AGV 与人员的厘米级轨迹追踪
物联网·算法·物流系统·快递系统·跨境物流快递系统·物联网终端实战