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); ... 
随机推荐
- vue老项目升级vue-cli3.0
			第一步我们卸载全局的vue2.0然后: 打开命令行 输入npm install -g @vue/cli-init 这个就是会安装全局的vue3.0版本.安装好之后我们也可以vue -V查看当前vu ... 
- Mahmoud and Ehab and the message
			Mahmoud wants to send a message to his friend Ehab. Their language consists of n words numbered from ... 
- SQL With As的用法
			WITH AS,也叫子查询部分(subquery factoring),可以定义一个SQL片断,该SQL片断会被整个SQL语句用到.可以使SQL语句的可读性更高,也可以在UNION ALL的不同部分, ... 
- jquery validate验证插件扩展方法(转)
			/***************************************************************** jQuery Validate扩展验证方法 (linjq) *** ... 
- C#中ESRI.ArcGIS.esriSystem的引用问题
			ESRI.ArcGIS.esriSystem,在引用里没有它的同名引用,其实它对应的引用为ESRI.ArcGIS.System,所以添加“ESRI.ArcGIS.System”这个引用即可 
- Docker+JMeter单机版+Nginx
			基于JMeter5.1.1+Nginx1.12.2JMeter发起压测 Nginx作为文件服务器 一.目录结构: Dockerfile文件: FROM ubuntu:18.04# 基础镜像 MAIN ... 
- php curl请求 header头携带参数
			$headers = array( 'api-key:'.$key, 'authorization:'.$authorization, ); //初始化 $curl = c ... 
- java基础(七)之子类实例化
			知识点;1.生成子类的过程2.使用super调用父类构造函数的方法 首先编写3个文件. Person.java class Person{ String name; int age; Person() ... 
- Word2010如何从指定页设置页码
			光标定位:将光标定位于需要开始编页码的页首位置. 插入分隔符的”下一页”:选择“页面布局—>分隔符—> 下一页”插入. 插入页码:选择“插入—>页码—> 页面底端”,选 ... 
- Java下载文件时文件名中的中文变成下划线,其他正常
			将 utf-8 转换成 ISO8859-1 编码 response.addHeader("Content-Disposition", "attachment;filena ... 
