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. Discuz的文件目录解析

    最近在学习discuz,对这个不了解的 可以参考 http://www.cnblogs.com/hgj123/p/4641845.html

  2. python学习03字符串基本操作

    '''字符串可以用单引号,双引号,三引号表示 '''#1.读取str1='I am a student!'#每一个字符对应一个下标,可以利用下标的方式来读取字符串对应的值——索引print(str1[ ...

  3. springboot中Redis的Lettuce客户端和jedis客户端

    1.引入客户端依赖 <!--jedis客户端依赖--> <dependency> <groupId>redis.clients</groupId> &l ...

  4. HDU 2513 Cake slicing

    #include<bits/stdc++.h> using namespace std; int n,m,k; int cherry[405],dp[405][405]; int solv ...

  5. office 365 激活

    将以下代码复制到记事本 @echo off title Activate Microsoft Office ALL versions &echo - Microsoft Office Prof ...

  6. liunx 之 Ubuntu 网速慢解决方法

    打开终端依次输入以下指令: sudo lshw -numeric -class network sudo ip addr show sudo ip route show sudo tracepath ...

  7. Linux分类

    Linux versions:http://www.cnblogs.com/sammyliu/articles/4832157.html1. Maintained by organization- D ...

  8. 由JS数组去重说起

    一.问题描述: var array=[1,45,3,1,4,67,45],请编写一个函数reDup来去掉其中的重复项,即 reDup(array); console.log(array);//[1,4 ...

  9. bfs—Dungeon Master—poj2251

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32228   Accepted: 12378 ...

  10. 你这些知识点都不会,你学个锤子SQL数据库!

    全套的数据库的知识都在这里,持续更新中ing 快戳我查看,快戳戳,不管是Oracle还是mysql还是sqlsever,SQL语言都是基础. 一.关系 单一的数据结构----关系 现实世界的实体以及实 ...