2019 徐州网络赛 G Colorful String 回文树
题目链接:https://nanti.jisuanke.com/t/41389
The value of a string sss is equal to the number of different letters which appear in this string.
Your task is to calculate the total value of all the palindrome substring.
Input
The input consists of a single string ∣s∣(1≤∣s∣≤3×105)|s|(1 \le |s| \le 3 \times 10^5)∣s∣(1≤∣s∣≤3×105).
The string sss only contains lowercase letters.
Output
Output an integer that denotes the answer.
样例输入
- abac
样例输出
- 6
样例解释
abac has palindrome substrings a,b,a,c,aba,ans the total value is equal to 1+1+1+1+2=6
题意:对于给定的字符串,求每个子回文串中不同的字符个数之和。
题解:回文树建树求出每不同的回文串出现的次数,再用DFS(开始节点,字母种类),累加求不同字符个数之和
- #include<iostream>
- #include<stdio.h>
- #include <algorithm>
- #include <string>
- #include<string.h>
- #include<math.h>
- #define ll long long
- using namespace std;
- const int MAXN = ;
- const int N = ;
- char s[MAXN],ss[MAXN];//输入的要处理的字符串
- ll ans=;
- int vis[];
- struct Palindromic_Tree
- {
- int next[MAXN][] ;//next指针,next指针和字典树类似,指向的串为当前串两端加上同一个字符构成
- int fail[MAXN] ;//fail指针,失配后跳转到fail指针指向的节点
- //回文树里面的一个节点就代表一个回文串
- int cnt[MAXN] ;//表示第i个节点代表的回文串出现的次数
- int num[MAXN] ; //表示以节点i表示的最长回文串的最右端点为回文串结尾的回文串个数。
- int len[MAXN] ;//表示第i个节点代表的回文串长度
- int S[MAXN] ;//存放添加的字符
- int last ;//指向上一个字符所在的节点,方便下一次add
- int n ;//字符数组指针
- int p ;//节点指针
- int newnode(int l) //在树中新建节点
- {
- for(int i = ; i < N ; ++ i) next[p][i] = ;
- cnt[p] = ;
- num[p] = ;
- len[p] = l ;
- return p ++ ;
- }
- void init() //初始化
- {
- p = ;
- newnode() ;//建一棵保存长度为偶数的回文树
- newnode(-) ;//长度为奇数的回文树
- last = ;
- n = ;
- S[n] = - ;//开头放一个字符集中没有的字符,减少特判
- fail[] = ;
- }
- int get_fail(int x) //和KMP一样,失配后找一个尽量最长的
- {
- while(S[n - len[x] - ] != S[n])
- x = fail[x] ;
- return x ;
- }
- void add(int c,int pos)
- {
- //printf("%d:",p);//------------>输出节点编号,第一个有回文串的编号时从2开始
- c -= 'a';
- S[++ n] = c ;
- int cur = get_fail(last) ; //通过上一个回文串找这个回文串的匹配位置
- //printf("%d ",cur);//输出节点编号p代表的回文串的字符长度
- if(!next[cur][c]) //如果这个回文串没有出现过,说明出现了一个新的本质不同的回文串
- {
- int now = newnode(len[cur] + ) ; //新建节点
- fail[now] = next[get_fail(fail[cur])][c] ; //和AC自动机一样建立fail指针,以便失配后跳转
- next[cur][c] = now ;
- num[now] = num[fail[now]] + ;
- }
- last = next[cur][c] ;
- cnt[last] ++ ;
- //putchar(10);//------------->输出回车换行
- }
- void count()
- {
- for(int i = p - ; i >= ; -- i)
- cnt[fail[i]] += cnt[i] ;
- //父亲累加儿子的cnt,因为如果fail[v]=u,则u一定是v的子回文串!
- }
- void dfs(int x,int y)
- {
- for(int i=;i<;i++)
- {
- if(next[x][i])
- {
- int z;//回文串不同字母的个数
- if(vis[i])//vis标记回文串出现字母的种类
- {
- z=y;
- ans=ans+z*cnt[next[x][i]];
- dfs(next[x][i],z);
- }
- else
- {
- vis[i]++;
- z=y+;
- ans=ans+z*cnt[next[x][i]];
- dfs(next[x][i],z);
- vis[i]--;//回溯
- }
- }
- }
- }
- } pat;
- int main()
- {
- ans=;
- scanf("%s",s);
- int n=strlen(s);
- pat.init();
- for(int i=; i<n; i++)
- pat.add(s[i],i);
- pat.count();
- pat.dfs(,);//长度为偶数的回文树
- memset(vis,,sizeof(vis));
- pat.dfs(,);//长度为奇数的回文树
- printf("%lld\n",ans);
- return ;
- }
2019 徐州网络赛 G Colorful String 回文树的更多相关文章
- The Preliminary Contest for ICPC Asia Xuzhou 2019 G. Colorful String 回文树
签到提: 题意:求出每一个回文串的贡献 (贡献的计算就是回文串不同字符的个数) 题解: 用回文树直接暴力即可 回文树开一个数组cost[ ][26] 和val[ ] 数组: val[i]表示回文树上节 ...
- ICPC 2019 徐州网络赛
ICPC 2019 徐州网络赛 比赛时间:2019.9.7 比赛链接:The Preliminary Contest for ICPC Asia Xuzhou 2019 赛后的经验总结 // 比赛完才 ...
- HDU 5157 Harry and magic string(回文树)
Harry and magic string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
- HDU 6599 I Love Palindrome String (回文树+hash)
题意 找如下子串的个数: (l,r)是回文串,并且(l,(l+r)/2)也是回文串 思路 本来写了个回文树+dfs+hash,由于用了map所以T了 后来发现既然该子串和该子串的前半部分都是回文串,所 ...
- ACM-ICPC 2018 南京赛区网络预赛 I. Skr(回文树)
题意 https://nanti.jisuanke.com/t/A1955 求所有本质不同的回文串转成数后的和. 思路 如果了解回文树的构造原理,那么这题就很简单了,回文树每个结点代表一个回文串,每添 ...
- query 2019徐州网络赛(树状数组)
query \[ Time Limit: 2000 ms \quad Memory Limit: 262144 kB \] 题意 补题才发现比赛的时候读了一个假题意.... 给出长度为 \(n\) 的 ...
- 2019南昌网络赛G. tsy's number
题意:\(\sum_{i=1}^n\sum_{j=1}^n\sum_{k=1}^n\frac{\phi(i)*\phi(j^2)*\phi(k^3)}{\phi(i)*\phi(j)*\phi(k)} ...
- 2019徐州网络赛H :function (min25筛)
题意:f(i)=i的幂次之和. 求(N+1-i)*f(i)之和. 思路:可以推论得对于一个素数p^k,其贡献是ans=(N+1)[N/(P^k)]+P^k(1+2+3...N/(P^k)); 我们分两 ...
随机推荐
- Go_排序
package main import ( "fmt" "sort" "math/rand" ) //1.声明Hero结构体 type He ...
- (转)基于快速排序的TOPK算法
基于快速排序的TOPK算法 转自:http://blog.csdn.net/fanzitao/article/details/7617223 思想: 类似于快速排序,首先选择一个划分元,如果这个划分元 ...
- 7 scrapy 初识
scrapy框架 框架介绍: Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架就是一个已经被集成了各种功能(高性能异步下载,队列,分布式,解析,持久化 ...
- 【嵌入式】ARM9复习
一.嵌入式系统基础 二.ARM处理器 1. 在每条指令后,用;//注释这条指令的寻址方式,以及实现的功能(25分) 注:变址寻址需要标注是基址加偏移.还是基址加索引,是前变址还是后变址.SUB SP, ...
- Vue.nextTick DOM 更新循环结束之后执行延迟回调
在下次 DOM 更新循环结束之后执行延迟回调.在修改数据之后立即使用这个方法,获取更新后的 DOM. 简单来说,Vue 在修改数据后,视图不会立刻更新,而是等同一事件循环中的所有数据变化完成之后,再统 ...
- BUG搬运工:CSCun88303-CIMC Memory Leak : Can't SSH/HTTP to CIMC
Symptom:Unable to SSH/HTTP to the CIMC of the C-series server, however the CIMC can be pinged. Also ...
- Python学习第二十二课——Mysql 表记录的一些基本操作 (增删改)
记录基本操作: 增:(insert into) 基本语法: insert into 表名(字段) values(对应字段的值): 例子1: insert into employee(id,name,a ...
- StudentManagerSSM
web.xml StudentManagerSSM.rar <?xml version="1.0" encoding="UTF-8&quo ...
- 线程池三种队列使用,SynchronousQueue,LinkedBlockingQueue,ArrayBlockingQueue
使用方法: private static ExecutorService cachedThreadPool = new ThreadPoolExecutor(4, Runtime.getRuntime ...
- JAVA GUI窗体及控件
Swing基本操作: JAVA显示一个带按钮的窗口: import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyB ...