当前位置: 首页 > news >正文

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

题目

题解

#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;
}


http://www.mrgr.cn/news/54267.html

相关文章:

  • 《MYSQL实战45讲 》 优化器如何选择索引?
  • 【STM32学习】PWM学习(四),散热风扇的控制,PWM调速调制,
  • 面试后的想法
  • JAVA课设-图书指引系统(前后端分离)
  • 《Redis实战》note-8 构建简单的社交网站
  • 排序02 Multi-gate Mixture-of-Experts (MMoE)
  • 【H2O2|全栈】关于CSS(14)如何完成常规的页面布局
  • 基于机器学习的混凝土抗压强度及利用Docker与FastAPI进行模型部署并形成API
  • 鸿蒙应用开发中,实现文件上传功能
  • 查询网站在线人数
  • Python基础09_类和对象(下)迭代器和生成器函数式编程
  • UEFI 基础教程 (四十八.2) — UEFI code style
  • org.apache.http.impl.client.CloseableHttpClient的时候如果发生异常
  • 《使用Gin框架构建分布式应用》阅读笔记:p88-p100
  • 群控系统服务端开发模式-功能整理
  • 【移动安全】OWASP MASTG 移动应用程序安全测试指南
  • 大模型~合集14
  • 理解 React 中的 ReactElement、children 和 ReactNode
  • Java 线程池获取池中所有线程列表的方法
  • 优化方法之随机梯度下降SGD优化器收敛性证明
  • 代码随想录day04
  • mysql connect -- C api编译链接问题,接口介绍(初始化和销毁,连接,执行sql语句,获取结果集的元数据和数据,设置编码格式)
  • Python Logging 模块
  • Unexpected error: java.security.InvalidAlgorithmParameterException
  • 关于office中的word文档图片替换问题
  • MySQL程序介绍<二>