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); ...
随机推荐
- 忘记本地MySQL数据库密码的解决方法
平台:win7 1.打开cmd窗口,进入 MySQL的安装目录. 2.停止MySQL的服务.已经停止了. 右键MySQL,看到启动是可点击的,证明MySQL服务已停止运行. 3.将root用户对数据库 ...
- PHP毫秒
PHP毫秒 php的毫秒是没有默认函数的,但提供了一个microtime()函数,该函数返回包含两个元素,一个是秒数,一个是小数表示的毫秒数,借助此函数,可以很容易定义一个返回毫秒数的函数,例如: ...
- Python中super的用法【转载】
Python中super的用法[转载] 转载dxk_093812 最后发布于2019-02-17 20:12:18 阅读数 1143 收藏 展开 转载自 Python面向对象中super用法与MRO ...
- console.log对象全部展开
挖掘Chrome Console的小秘密 SP_lyu关注 2018.09.15 18:25:32字数 1,697阅读 917 控制台应该是大多数前端开发人员日常开发调试离不开的神器.然而控制台仍有很 ...
- pycharm项目移植过程中遇到的问题
调试中遇到三个问题: 问题1:Error running 'run_all_test': Cannot run program "C:\Users\Administrator\.virtu ...
- youtube-dl parameters
how to select the format of vidoes from youtube-dl? https://www.jianshu.com/p/611009843919 https://u ...
- python property(不动产)方法
class Test(object): @property def test(self): return 100 @test.setter def test(self): return "修 ...
- ajax中的参数
function login() { $.ajax({ //几个参数需要注意一下 type: "POST",//方法类型 dataType: "json",// ...
- document.getElementById("id").value与$("#id").val()之间的区别
本文链接:https://blog.csdn.net/mottohlm/article/details/78364196....今天在项目中遇到这么一个JS报错:原因是代码中有这么一段:对,就是var ...
- java多线程--死锁
1. Java中导致死锁的原因 Java中死锁最简单的情况是,一个线程T1持有锁L1并且申请获得锁L2,而另一个线程T2持有锁L2并且申请获得锁L1,因为默认的锁申请操作都是阻塞的,所以线程T1和T2 ...