Colorful String
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e6+;
char s[maxn];
int n;
int record[maxn]; // 记录i结点在原字符串的位置
int sum[maxn*];
void pushup(int rt) {
sum[rt] |= sum[rt*];
sum[rt] |= sum[rt*+];
}
void build(int l, int r, int rt) {
if (l == r) {
sum[rt] |= <<(s[l]-'a');
return;
}
int mid = (l+r)/;
build(l,mid,rt*);
build(mid+,r,rt*+);
pushup(rt);
}
int query(int be, int ed, int l, int r, int rt) {
if (be <= l && r <= ed) {
return sum[rt];
}
int mid = (l+r)/, res = ;
if (be <= mid) res |= query(be,ed,l,mid,rt*);
if (ed > mid) res |= query(be,ed,mid+,r,rt*+);
return res;
} struct PAM {
int last;
struct Node {
ll cnt, len, fail, son[]; // cnt为以i为结尾的回文子串个数,len为长度
Node(int len, int fail) : len(len), fail(fail), cnt(){
memset(son, , sizeof(son));
};
};
vector<Node> st;
inline int newnode(int len, int fail = ) {
st.emplace_back(len, fail);
return st.size()-;
}
inline int getfail(int x, int n) {
while (s[n-st[x].len-] != s[n]) x = st[x].fail;
return x;
}
inline void extend(int c, int i) {
int cur = getfail(last, i);
if (!st[cur].son[c]) {
int nw = newnode(st[cur].len+, st[getfail(st[cur].fail, i)].son[c]);
st[cur].son[c] = nw;
}
st[ last=st[cur].son[c] ].cnt++;
record[last] = i;
}
void init() {
scanf("%s", s+);
n = strlen(s+);
s[] = ;
newnode(, ), newnode(-);
last = ;
for (int i = ; i <= n; i++)
extend(s[i]-'a', i);
}
ll count() {
for (int i = st.size()-; i >= ; i--)
st[st[i].fail].cnt += st[i].cnt; ll ans = n;
for (int i = ; i <= st.size()-; i++) {
if (st[i].len <= ) continue; int L = record[i]-st[i].len+, R = record[i];
int res = query(L,R,,n,);
int num = ;
while (res) {
if (res&) num++;
res >>= ;
}
ans += st[i].cnt*num;
}
return ans;
}
}pam;
int main() {
pam.init();
build(,n,);
printf("%lld\n",pam.count());
return ;
}
Colorful String的更多相关文章
- 【Manacher】Colorful String
The value of a string s is equal to the number of different letters which appear in this string. You ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- 2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...
- G.Colorful String(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/4 #include <bits/stdc++.h> using namespace std; ,; typedef unsign ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)
这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...
- The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551
地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...
- 马拉车manacher
目的:线性查找一个串的最长回文子串 时间复杂度:O(n) len[i]表示以i为中心的回文串的半径,mx即为当前计算回文串最右边字符的最大值,p是中心点mid,mx-i和2*p-1关于p对称 http ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019
A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...
- 2019icpc-徐州网络赛
B. hxc写的 AC code: #pragma GCC optimize(2) #include <cstdio> #include <queue> #include &l ...
随机推荐
- 手机照片的exif里有方向属性
<?php $image = imagecreatefromstring(file_get_contents($_FILES['image_upload']['tmp_name'])); $ex ...
- QMessage自动定时关闭
QMessageBox *box = new QMessageBox(QMessageBox::Information,tr("test"),tr("testtest&q ...
- Inno Setup 删除文件夹 DelTree
Pascal Scripting: DelTree Prototype: function DelTree(const Path: String; const IsDir, DeleteFiles, ...
- 讲讲python中函数的参数
python中函数的参数 形参:定义函数时代表函数的形式参数 实参:调用函数时传入的实际参数 列如: def f(x,y): # x,y形参 print(x, y) f(1, 2) # 1, 2 实参 ...
- 压缩工具gzip、bzip2、xz的使用
2019独角兽企业重金招聘Python工程师标准>>> 本文使用 为了要压缩 常见压缩格式 压缩工具 gzip压缩工具 bz2压缩工具 xz压缩工具 为什么要压缩 为什么要压缩?文件 ...
- 腾讯技术工程 | 基于Prophet的时间序列预测
预测未来永远是一件让人兴奋而又神奇的事.为此,人们研究了许多时间序列预测模型.然而,大部分的时间序列模型都因为预测的问题过于复杂而效果不理想.这是因为时间序列预测不光需要大量的统计知识,更重要的是它需 ...
- 全面解读 vue3.0
在此附上链接,在知乎上看到的,感觉说的很详细 https://zhuanlan.zhihu.com/p/46269528
- Spring Cloud 学习 之 Spring Cloud Ribbon(基础知识铺垫)
文章目录 1.负载均衡: 2.RestTemplate详解: xxxForEntity/xxxForObject:主要介绍get跟post exchange: execute源码分析: 1.负载均衡: ...
- XSS检测总结
XSS漏洞介绍 跨站脚本XSS是一种针对网站应用程序的安全漏洞攻击技术.恶意攻击者往web页面插入恶意的Script代码,当用于浏览该页时,嵌入web中的恶意代码就会被执行,从而达到恶意攻击用 ...
- 【Kafka】Kafka集群基础操作!新手上路必备~
目录 bin目录 Topic命令概览 创建Topic 查看Topic 描述Topic Producer生产数据 Consumer消费数据 增加topic分区数 增加配置 删除配置 删除Topic 所有 ...