杭电多校HDU 6599 I Love Palindrome String (回文树)题解
题意:
定义一个串为\(super\)回文串为:
\(\bullet\) 串s为主串str的一个子串,即\(s = str_lstr_{l + 1} \cdots str_r\)
\(\bullet\) 串s为回文串
\(\bullet\) 串\(str_lstr_{l + 1}...str_{\llcorner (l + r) / 2 \lrcorner}\)也是回文串
问长度为1、2、3 \(\cdots n\)的\(super\)回文串分别出现了几次
思路:
回文树建一下,然后每次新建一个节点的时候用hash快速判断一下是不是\(super\)回文串,然后回文树统计一下个数。
代码:
#include<map>
#include<set>
#include<cmath>
#include<cstdio>
#include<stack>
#include<ctime>
#include<vector>
#include<queue>
#include<cstring>
#include<string>
#include<sstream>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const ull seed = 131;
const ll MOD = 1e9 + 7;
using namespace std;
ull ha[maxn], fac[maxn];
int ans[maxn];
ull getstring(int l, int r){
return ha[r] - ha[l - 1] * fac[r - l + 1];
}
struct PAM{
int nex[maxn][26]; //指向的一个字符的节点
int fail[maxn]; //失配节点
int len[maxn]; //当前节点回文长度
int str[maxn]; //当前添加的字符串
int cnt[maxn]; //节点出现次数
int last;
int tot; //PAM中节点数
int N; //添加的串的个数
int satisfy[maxn];
int newnode(int L){
for(int i = 0; i < 26; i++) nex[tot][i] = 0;
len[tot] = L;
cnt[tot] = 0;
return tot++;
}
void init(){
tot = 0;
newnode(0);
newnode(-1);
last = 0;
N = 0;
str[0] = -1;
fail[0] = 1;
}
int getfail(int x){
while(str[N - len[x] - 1] != str[N]) x = fail[x];
return x;
}
void add(char ss){
int c = ss - 'a';
str[++N] = c;
int cur = getfail(last);
if(!nex[cur][c]){
int now = newnode(len[cur] + 2);
fail[now] = nex[getfail(fail[cur])][c];
nex[cur][c] = now;
int need = (len[now] + 1) / 2;
if(len[now] == 1 || getstring(N - len[now] + 1, N - len[now] + need) == getstring(N - need + 1, N)) satisfy[now] = 1;
else satisfy[now] = 0;
}
last = nex[cur][c];
cnt[last]++;
}
void count(){
for(int i = tot - 1; i >= 0; i--){
cnt[fail[i]] += cnt[i];
if(satisfy[i]) ans[len[i]] += cnt[i];
}
}
}pa;
char s[maxn];
int main(){
fac[0] = 1;
for(int i = 1; i < maxn; i++) fac[i] = fac[i - 1] * seed;
while(~scanf("%s", s + 1)){
pa.init();
int len = strlen(s + 1);
ha[0] = 1;
for(int i = 1; i <= len; i++){
ha[i] = ha[i - 1] * seed + s[i];
}
for(int i = 1; i <= len; i++) ans[i] = 0;
for(int i = 1; i <= len; i++){
pa.add(s[i]);
}
pa.count();
for(int i = 1; i <= len; i++){
if(i != 1) printf(" ");
printf("%d", ans[i]);
}
puts("");
}
return 0;
}
杭电多校HDU 6599 I Love Palindrome String (回文树)题解的更多相关文章
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- 杭电多校HDU 6579 Operation (线性基 区间最大)题解
题意: 强制在线,求\(LR\)区间最大子集异或和 思路: 求线性基的时候,记录一个\(pos[i]\)表示某个\(d[i]\)是在某个位置更新进入的.如果插入时\(d[i]\)的\(pos[i]\) ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU.5394.Trie in Tina Town(回文树)
题目链接 \(Description\) 给定一棵\(Trie\).求\(Trie\)上所有回文串 长度乘以出现次数 的和.这里的回文串只能是从上到下的一条链. 节点数\(n\leq 2\times ...
- [2019杭电多校第二场][hdu6599]I Love Palindrome String(回文自动机&&hash)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6599 题目大意为求字符串S有多少个子串S[l,r]满足回文串的定义,并且S[l,(l+r)/2]也满足 ...
- 杭电多校HDU 6656 Kejin Player(概率DP)题解
题意: 最低等级\(level\ 1\),已知在\(level\ i\)操作一次需花费\(a_i\),有概率\(p_i\)升级到\(level\ i+1\),有\(1 - p_i\)掉级到\(x_i( ...
- 杭电多校HDU 6601 Keen On Everything But Triangle(主席树)题解
题意: 有\(n\)根长度不一的棍子,q次询问,求\([L,R]\)区间的棍子所能组成的周长最长的三角形.棍长\(\in [1, 1e9]\),n\(\in [1, 1e5]\). 思路: 由于不构成 ...
- 杭电多校HDU 6586 String(预处理 + 贪心)题解
题意: 给你一个串,现需要你给出一个子序列,满足26个约束条件,\(len(A_i) >= L_i\) 且 \(len(A_i) <= R_i\), \(A_i\)为从a到z的26个字母. ...
- [2019杭电多校第三场][hdu6609]Find the answer(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6609 大致题意是求出每个位置i最小需要将几个位置j变为0(j<i),使得$\sum_{j=1}^ ...
随机推荐
- LTH7锂电池充放电IC完整方案
内容目录: A,LTH7贴片5脚充电芯片 PW4054 1, 单节的锂电池保护电路 单节为3.7V锂电池(也叫4.2V)和3.8V锂电池(也叫4.35V) 2, 单节的锂电池充电电路 ...
- 三节锂电池充电管理芯片,IC电路图如何设计
关于三节锂电池供电的产品,在三节锂电池上,需要三个电路系统: 1,三节锂电池保护电路, 2,三节锂电池充电电路, 3,三节锂电池输出电路. 1.三节锂电池保护电路,芯片电路图 控制三节锂电池池的充电电 ...
- LSM(Log Structured Merge Trees ) 笔记
目录 一.大幅度制约存储介质吞吐量的原因 二.传统数据库的实现机制 三.LSM Tree的历史由来 四.提高写吞吐量的思路 4.1 一种方式是数据来后,直接顺序落盘 4.2 另一种方式,是保证落盘的数 ...
- uni-app开发经验分享十八:对接第三方h5
1.uni-app中对接第三方为了防止跳出app使用了webview <template> <view> <web-view :src="url" @ ...
- MATLAB中load和imread的读取方式区别
load是导入文件,一般从mat文件中,读取的是结构体imread是图像处理工具箱的库函数,处理图像比较方便,读取的是矩阵 1.之前将数组或者矩阵保存为一个mat格式的文件,在进行load命令读取时: ...
- (12)-Python3之--openpyxl模块
1.安装 pip install openpyxl 2.Excel操作的流程 1.打开excel,进入工作薄 workbook2.选择表单 Sheet 3.单元格 Cell4.读写操作 3.Exce ...
- (01)-Python3之--字符串操作
1.字符串切片取值 字符串的取值通过索引来读取,从0开始. 取区间值如下:字符串变量名[起始索引:结束索引].包含起始,但不包含结束.例如: str_my = "hello,python!我 ...
- 在Sublime Text 2工具下编辑laravel框架
介绍Sublime编辑器 Sublime Text 3官方版是Sublime Text2的升级版.Sublime Text是一款流行的文本编辑器软件,有点类似于TextMate,跨平台,可运行在Lin ...
- 非Windows系统 如何解压带中文密码和中文文件名的zip压缩文件
数据科学交流群,群号:189158789 ,欢迎各位对数据科学感兴趣的小伙伴的加入! 一.安装unar软件包: Linux(Debian系列): apt install unarLinux(RedHa ...
- Xctf-easyapk
Xctf-easyapk Write UP 前期工作 查壳 无壳 运行 没什么特别的 逆向分析 使用jadx反编译查看代码 先看看文件结构 MainActivity代码 public class Ma ...