The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)
这题建立一棵回文树,然后用dfs搜索答案,但是有一点需要注意,就是打vis的标记时,如果标记为1,那么在好几个节点都对同一个字符i打过标记,此时的搜索从字符i点回溯,回到它的父亲节点,搜索其它的字符,回溯的时候把vis[i]标记成0了,之前的vis[i]标记全被清空了,如果该父亲的其它字符节点下,有字符i的孩子,则此时统计就会出错。所以打vis标记的时候让vis++,而不是标记为0。
#include <iostream>
#include <stdio.h>
using namespace std;
const int MAXN=3e5+10;
const int N=26;
char str[MAXN];
struct PAM {
int len[MAXN];
int num[MAXN];
int s[MAXN];
int fail[MAXN];
int next[MAXN][N];
int cnt[MAXN];
int last,p,n;
int newNode(int l) {
for (int i=0;i<N;i++) next[p][i]=0;
cnt[p]=0;
num[p]=0;
len[p]=l;
return p++;
}
void init() {
n=0;
p=0;
last=0;
newNode(0);
newNode(-1);
fail[0]=1;
s[0]=-1;
}
int getFail(int x) {
while (s[n-len[x]-1]!=s[n]) x=fail[x];
return x;
}
void add(int c) {
c-='a';
s[++n]=c;
int cur=getFail(last);
if (!next[cur][c]) {
int now=newNode(len[cur]+2);
fail[now]=next[getFail(fail[cur])][c];
next[cur][c]=now;
num[now]=num[fail[now]]+1;
}
last=next[cur][c];
cnt[last]++;
}
void count() {
for (int i=p-1;i>=0;i--) {
cnt[fail[i]]+=cnt[i];
}
}
}pt;
int vis[N];
long long ans=0;
void dfs(int x,int cnt)
{
ans+=cnt*pt.cnt[x];
for (int i=0;i<N;i++) {
if (pt.next[x][i]) {
if (!vis[i]) {
vis[i]++;
dfs(pt.next[x][i],cnt+1);
vis[i]--;
}
else {
dfs(pt.next[x][i],cnt);
}
}
}
}
int main()
{
pt.init();
scanf("%s",str);
for (int i=0;str[i];i++) {
pt.add(str[i]);
}
pt.count();
// printf("%d\n",pt.cnt[0]);
dfs(0,0);
dfs(1,0);
printf("%lld\n",ans);
return 0;
}
The Preliminary Contest for ICPC Asia Xuzhou 2019 G Colorful String(回文自动机+dfs)的更多相关文章
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)
query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]
也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019
A:Who is better? 题目链接:https://nanti.jisuanke.com/t/41383 题意: 类似于有N个石子,先手第一次不能拿完,每次后手只能拿 1 到 前一次拿的数量* ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 【 题目:so easy】{并查集维护一个数的下一个没有被删掉的数} 补题ING
题意:给[1,n],n个数,有两种操作: 1 x,删去x2 x,查询还未被删去的数中大于等于x的最小的数是多少. input: output: 做法:按照并查集的方法压缩路径 代码: #include ...
- 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 ...
- E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)
https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...
随机推荐
- python 多版本环境
参考 https://www.cnblogs.com/---JoyceLiuHome/articles/7852871.html 安装 Anaconda集成化环境 https://www.anacon ...
- my bug of VG algorithm
def visibility_graph(series): g = nx.Graph() # convert list of magnitudes into list of tuples that h ...
- python3 win 建立虚拟环境(virtualenv)
1.安装virtualenv pip3 install virtualenv 2.进入即将创建虚拟环境的目录 cd xxxx 3.创建虚拟环境 py -3 -m venv testxunihua 4. ...
- LitElement(二)模板编写基本语法
原文:https://lit-element.polymer-project.org/guide/templates 1.定义一个渲染模板 1.1 基本规则 要用LitElement 组件定义一个模板 ...
- 通过java代码HttpRequestUtil(服务器端)发送HTTP请求并解析
关键代码:String jsonStr = HttpRequestUtil.sendGet(config.getAddress() + config.getPorts() + config.getFi ...
- web自动化环境搭建(python+selenium+webdriver)
本文档以谷歌浏览器为例,故自动化测试环境为下: 自动化工具为:selenium+webdriver 脚本语言为:Python3.X 浏览器:Chrome 系统环境:Win10 编译工具:Pycharm ...
- JAVA变量声明在循环体内还是循环体外
(1) for (int i = 0; i < 10000; ++i) { Object obj = new Object(); System.out.println("obj= &q ...
- LeetCode第144场周赛总结
5117.IP地址无效化 首先计算出输入IP地址的长度,然后遍历每一个字符. 如果当前字符为'.',就在它的前后两侧分别加上'['和']'字符. 值得一提的是,C++的String类型提供了以上操作的 ...
- 并发队列 ConcurrentLinkedQueue 及 BlockingQueue 接口实现的四种队列
队列是一种特殊的线性表,它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作.进行插入操作的端称为队尾,进行删除操作的端称为队头.队列中没有元素时,称为空队列. 在队列这 ...
- Unity手机端手势基本操作
主要有单指移动3D物体.单指旋转3D物体.双指缩放3D物体. 基类 using UnityEngine; using System.Collections; /// <summary> / ...