当前位置:网站首页>PAM Palindromic Automata
PAM Palindromic Automata
2022-08-01 22:07:00 【ThXe】
性质
1.本质不同(The location can be the same)The number of palindromic strings is equal to the number of nodes in the palindromic automaton − 2 ;
2.The number of occurrences of a palindrome is equal to the number of nodes in the subtree rooted at it lastsum of times;
3.The number of palindromes with different positions is equal to each node(除 even 和 odd)The sum of the occurrences of the corresponding palindrome;
功能
1.Find the number of essentially distinct palindromes
2.Find the number of times a string appears as a palindrome
3.以iThe sign character is the number of ending palindrome strings
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
char str[maxn];
struct PAM {
int size, last, r0, r1;
int trie[maxn][26], fail[maxn], len[maxn], cnt[maxn];//cnt[last]为iNo. node is the end
PAM() {
r0 = size++, r1 = size++; last = r1;
len[r0] = 0, fail[r0] = r1;
len[r1] = -1, fail[r1] = r1;
}
void insert(int ch, int idx) {
int u = last;
while (str[idx] != str[idx - len[u] - 1])u = fail[u];
if (!trie[u][ch]) {
int cur = ++size, v = fail[u];
len[cur] = len[u] + 2;
for (; str[idx] != str[idx - len[v] - 1]; v = fail[v]);
fail[cur] = trie[v][ch]; trie[u][ch] = cur;
cnt[cur] = cnt[fail[cur]] + 1;
}
last = trie[u][ch];
//cnt[last]++ Required to count the number of occurrences of a string
}
void build(char* str) {
int len = strlen(str);
for (int i = 0; i < len; i++)
insert(str[i] - 'a' + 1, i);
}
}pam;
int main() {
scanf("%s", str);
pam.build(str);
int len = strlen(str);
long long ans = 0;
//op1:Count the number of all palindrome strings
for (int i = 1; i <= pam.size; i++)
ans += pam.cnt[i];
printf("%d", ans);
//op2:输出iThe number of palindromes at the end
for (int i = 0; i < len; i++) {
pam.insert(str[i] - 'a' + 1, i);
printf("%d ", pam.cnt[pam.last]);
}
//op3:How many times a string appears,需要在buildwhen maintaining information
for (int i = pam.size; i >= 0; i--) {
pam.cnt[pam.fail[i]] += pam.cnt[i];
ans = max(ans, 1ll * pam.cnt[i] * pam.len[i]);
}
return 0;
}
边栏推荐
- 如何理解 new (...args: any[]) => any
- (翻译)按钮的对比色引导用户操作的方式
- 模拟数据之mockjs
- Safe fifth after-school exercise
- 【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读
- User Experience | How to Measure User Experience?
- leetcode 204. Count Primes 计数质数 (Easy)
- 2022-08-01 第八组 曹雨 泛型 枚举
- [深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
- 2022 edition of MySQL tutorial, top collection good, take your time
猜你喜欢
小程序毕设作品之微信体育馆预约小程序毕业设计成品(3)后台功能
使用Jenkins做持续集成,这个知识点必须要掌握
迁移学习——Discriminative Transfer Subspace Learning via Low-Rank and Sparse Representation
shell programming conventions and variables
Based on php online examination management system acquisition (php graduation design)
Unity Shader general lighting model code finishing
小程序毕设作品之微信美食菜谱小程序毕业设计成品(5)任务书
【建议收藏】ヾ(^▽^*)))全网最全输入输出格式符整理
【C语言实现】两种计算平均成绩题型,博主精心整理,值得一读
【牛客刷题-SQL大厂面试真题】NO4.出行场景(某滴打车)
随机推荐
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
感觉自己好傻
shell规范与变量
威纶通触摸屏如何打开并升级EB8000旧版本项目并更换触摸屏型号?
越长大越孤单
使用分类权重解决数据不平衡的问题
RxJs SwitchMapTo 操作符之移花接木
Raspberry Pi information display small screen, display time, IP address, CPU information, memory information (C language), four-wire i2c communication, 0.96-inch oled screen
力扣第 304 场周赛复盘
How to prevent governance attacks in DAOs?
Unity Shader general lighting model code finishing
shell programming conventions and variables
HCIP---Multiple Spanning Tree Protocol related knowledge points
入门数据库Days4
ModuleNotFoundError: No module named 'yaml'
SOM网络2: 代码的实现
2022-08-01 第八组 曹雨 泛型 枚举
The must-have "fishing artifact" for programmers is here!
毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
教你VSCode如何快速对齐代码、格式化代码