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的更多相关文章

  1. 【Manacher】Colorful String

    The value of a string s is equal to the number of different letters which appear in this string. You ...

  2. The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树

    签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...

  3. 2019 徐州网络赛 G Colorful String 回文树

    题目链接:https://nanti.jisuanke.com/t/41389 The value of a string sss is equal to the number of differen ...

  4. 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 ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)

    这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符, ...

  6. 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 ...

  7. 马拉车manacher

    目的:线性查找一个串的最长回文子串 时间复杂度:O(n) len[i]表示以i为中心的回文串的半径,mx即为当前计算回文串最右边字符的最大值,p是中心点mid,mx-i和2*p-1关于p对称 http ...

  8. The Preliminary Contest for ICPC Asia Xuzhou 2019

    A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...

  9. 2019icpc-徐州网络赛

    B. hxc写的 AC code: #pragma GCC optimize(2) #include <cstdio> #include <queue> #include &l ...

随机推荐

  1. DZ注册登录流程梳理

    用户注册流程第一步:/source/class/class_member.php: on_register注册入口 if(!$activation) {//不为空,说明用户已经注册过 //将用户注册到 ...

  2. linux uniq 命令实用手册

    Linux uniq 命令用于处理文本内容中的重复行. 这里我们只介绍其常用参数,其完整用法可参见man uniq. 例如,我们有如下文件内容: >>> cat log.txt __ ...

  3. c语言实现乘法口诀表

    利用c语言实现乘法口诀表的两种输出: #include <stdio.h> int main(int argc, char *argv[]) { ; ;a<;a++){//输出1-9 ...

  4. 【JAVA基础】04 Java语言基础:方法

    1. 方法概述和格式说明 为什么要有方法 提高代码的复用性 什么是方法 完成特定功能的代码块. 方法的格式 修饰符 返回值类型 方法名(参数类型 参数名1,参数类型 参数名2...) {     方法 ...

  5. 用两张图告诉你,为什么你的App会卡顿?

    有什么料? 从这篇文章中你能获得这些料: 知道setContentView()之后发生了什么? 知道Android究竟是如何在屏幕上显示我们期望的画面的? 对Android的视图架构有整体把握. 学会 ...

  6. ffmpeg+SDL2实现的音频播放器V2.0(无杂音)

    1. 前言 目前为止,学习了并记录了ffmpeg+SDL2显示视频以及事件(event)的内容. 这篇中记录ffmpeg+SDL2播放音频,没加入事件处理. 接下来加入事件处理并继续学习音视频同步,再 ...

  7. 图论--SCC缩点--Tarjan

    // Tarjan算法求有向图强连通分量并缩点 /*强连通缩点与双连通缩点大同小异,也就是说将强连通分支缩成一个点之后,没有强连通,成为有向无环图,在对图进行题目的操作.*/ #include< ...

  8. Python2 与 Python3 的区别

    python解释器默认编码(python2与python3的区别一) python2 解释器默认编码:ascii python3 解释器默认编码:utf-8 输入(python2与python3的区别 ...

  9. python——append后的列表为什么打印出来为空

    关于python 列表append的用法: list = [] list = list.append("c") print(list) >>>None 正确写法应 ...

  10. J集合选数

    题意:求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中.(结果对1e9+1取模) 分析: 首先,什么样的数才会产生排斥呢?(选了这个 ...