当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Based on php online music website management system acquisition (php graduation design)
ImportError: `save_weights` requires h5py. Problem solved
入门数据库Days4
scikit-learn no moudule named six
10 Practical Uses of NFTs (NFT System Development)
ARFoundation入门教程U2-AR场景截图截屏
恒星的正方形问题
APP专项测试:流量测试
ImportError: `save_weights` requires h5py.问题解决
使用Jenkins做持续集成,这个知识点必须要掌握
随机推荐
小程序毕设作品之微信体育馆预约小程序毕业设计成品(4)开题报告
xctf攻防世界 Web高手进阶区 webshell
Today's sleep quality record 74 points
leetcode 204. Count Primes 计数质数 (Easy)
shell规范与变量
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
Based on php animation peripheral mall management system (php graduation design)
APP专项测试:流量测试
Uses of Anacoda
微软校园大使喊你来秋招啦!
VGUgarbage collector(垃圾回收器)的实现原理
[ASM] Bytecode Operation MethodWriter
图论——强连通分量缩点+拓扑排序
Small program -- subcontracting
基于 OData 模型和 JSON 模型的 SAP UI5 表格控件行项目的添加和删除实现
SAP Spartacus Accessibility E2E 端到端测试
shell编程规范与变量
毕业十年,财富自由:那些比拼命努力更重要的事,从来没人会教你
Lecture 3: Several common table field data types in MySQL database
selenium无头,防检测