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. scrollview 和 listview滑动冲突解决

    http://blog.csdn.net/wanghao200906/article/details/51084975 http://www.cnblogs.com/shitianzeng/artic ...

  2. 原生JS中获取位置的方案总结

    获取鼠标当前位置 clientY.clientX: 鼠标当前位置 相对于 浏览器可视区域顶部.浏览器可视区域左部 的位置: pageY.pageX: 鼠标当前位置 相对于 文档顶部.文档左部的位置: ...

  3. 苹果系统通过brew安装sshpass

    默认使用brew install sshpass会出现Warning: MD5 support is deprecated and will be removedin a future version ...

  4. 2019 Multi-University Training Contest 10 I Block Breaker

    Problem Description Given a rectangle frame of size n×m. Initially, the frame is strewn with n×m squ ...

  5. java socket实现服务端,客户端简单网络通信。Chat

    之前写的实现简单网络通信的代码,有一些严重bug.后面详细写. 根据上次的代码,主要增加了用户注册,登录页面,以及实现了实时显示当前在登录状态的人数.并解决一些上次未发现的bug.(主要功能代码参见之 ...

  6. CF1324D Pair of Topics

    好像题解里都是树状数组(起码我翻到的是 说一种cdq分治的(这应该算是cdq分治了 用cdq比较简单,所以可以作为一个练手题 cdq分治其实是一种模糊的思想,处理\([l,r]\)区间内,有多少\(( ...

  7. bootstrap栅格系统的使用

    bootstrap栅格系统的使用 bootstrap栅格系统的使用,主要分为四种方式 1.列组合  col-md-* 2.列偏移 col-md-offset-* 3.列嵌套  大列组合包含着小组合 4 ...

  8. Dreamoon Likes Coloring(模拟+构造)

    \(这题刚好撞到我的思路了,但是因为模拟......我看了几十遍测试数据....\) $首先当\sum_^m$小于n时一定无解 大于呢?那我们就要浪费一些区间(覆盖一些点,也就是多出来的点) 但是又不 ...

  9. 无线脉冲水表LoRaWAN方案芯片ASR6500S

    无线脉冲水表LoRaWAN方案 物联网是新一代信息技术的重要组成部分,也是"信息化"时代的重要发展阶段,在物联网飞速发展的今天,只有多技术融合的物联网解决方案才能够在不同的应用场景 ...

  10. Node.js中模块加载机制

    1.模块查找规则-当模块拥有路径但没有后缀时:(require(‘./find’)) require方法根据模块路径查找模块,如果是完整路径,直接引入模块: 如果模块后缀省略,先找同名JS文件,再找同 ...