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 ...
随机推荐
- php里的闭包函数
一个匿名函数内要获取外部的变量必须要使用use: $message = 'hello'; $example = function() use ($message){ var_dump($message ...
- 2019-2020-1 20199303《Linux内核原理与分析》第九周作业
进程的切换和一般执行过程 知识总结 操作系统原理中介绍了大量进程调度算法,这些算法从实现的角度看仅仅是从运行队列中选择一个新进程,选择的过程中运用了不同的策略而已. 对于理解操作系统的工作机制,反而是 ...
- 微软的 Sysinternals 系统管理工具包,例如可找出自动启动的流氓软件
作者:Kenny链接:https://www.zhihu.com/question/52157612/answer/153886419来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- 在 ubuntu20 上替换原有的源,解决下载软件慢的问题
处理方式 使用命令查看到所有的下载源 sudo gedit /etc/apt/sources.list 使用163的源替换掉原始的源 163:http://mirrors.163.com/ubuntu ...
- CodeForces - 1102A(思维题)
https://vjudge.net/problem/2135388/origin Describe You are given an integer sequence 1,2,-,n. You ha ...
- CodeForces - 224C. Bracket Sequence (栈模拟)简单做法
A bracket sequence is a string, containing only characters "(", ")", "[&quo ...
- P4768 [NOI2018]归程(kruskal 重构树)
洛谷P4768 [NOI2018]归程 LOJ#2718.「NOI2018」归程 用到 kruskal 重构树,所以先说这是个啥 显然,这和 kruskal 算法有关系 (废话 这个重构树是一个有点权 ...
- python selenium(定位方法)
一.定位方法 注意:元素属性必须唯一存在 #id定位 find_element_by_id() #name定位 find_element_by_name() #class_name定位 find_el ...
- 使用天祥TX-1C调试DS18B20温度传感器的收获
翻查DS18B20的DataSheet编写操作函数,其过程遇到了不少坎,记下来备查. 对于单总线的DS18B20芯片,首先严格按照时序图写出正确的“写0”.“写1”和“读0.1”的基础函数,再以此写出 ...
- Linux内核驱动学习(七)应用层直接操作GPIO
文章目录 简介 原理图 节点 设置为输出 设置为输入 映射关系 debugfs pwm demo 简介 前面通过libgpio的方式介绍了内核空间对GPIO进行操作的接口,其做了较好的封装,同时Lin ...