BZOJ 3676 [Apio2014]回文串(回文树)
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3676
【题目大意】
考虑一个只包含小写拉丁字母的字符串s。
我们定义s的一个子串t的"出现值"为t在s中的出现次数乘以t的长度。
求s的所有回文子串中的最大出现值。
【题解】
我们对给出串建立回文树,统计每个回文串出现次数和长度,相乘取组大即可
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=300010,S=26;
int all,son[N][S],fail[N],cnt[N],len[N],text[N],last,tot;
int newnode(int l){
for(int i=0;i<S;i++)son[tot][i]=0;
cnt[tot]=0,len[tot]=l;
return tot++;
}
void init(){
last=tot=all=0;
newnode(0),newnode(-1);
text[0]=-1,fail[0]=1;
}
int getfail(int x){
while(text[all-len[x]-1]!=text[all])x=fail[x];
return x;
}
void add(int w){
text[++all]=w;
int x=getfail(last);
if(!son[x][w]){
int y=newnode(len[x]+2);
fail[y]=son[getfail(fail[x])][w];
son[x][w]=y;
}cnt[last=son[x][w]]++;
}
void count(){for(int i=tot-1;~i;i--)cnt[fail[i]]+=cnt[i];}
char s[N];
int main(){
while(~scanf("%s",s)){
int n=strlen(s);
init();
for(int i=0;i<n;i++)add(s[i]-'a');
count(); long long ans=0;
for(int i=0;i<tot;i++)ans=max(ans,1LL*cnt[i]*len[i]);
printf("%lld\n",ans);
}return 0;
}
BZOJ 3676 [Apio2014]回文串(回文树)的更多相关文章
- BZOJ 3676: [Apio2014]回文串
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 2013 Solved: 863[Submit][Status ...
- bzoj 3676: [Apio2014]回文串 回文自动机
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 844 Solved: 331[Submit][Status] ...
- 字符串(马拉车算法,后缀数组,稀疏表):BZOJ 3676 [Apio2014]回文串
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- ●BZOJ 3676 [Apio2014]回文串
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=3676 题解: 后缀数组,Manacher,二分 首先有一个结论:一个串的本质不同的回文串的个 ...
- bzoj 3676 [Apio2014]回文串(Manacher+SAM)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题意] 给定一个字符串,定义一个串的权值为长度*出现次数,求最大权的回文子串. ...
- bzoj 3676: [Apio2014]回文串【回文自动机】
回文自动机板子 或者是SAM+manacher+倍增,就是manacher求本质不同回文串(让f++的串),然后在SAM倍增查询对应点出现次数 #include<iostream> #in ...
- BZOJ 3676 [Apio2014]回文串 (后缀自动机+manacher/回文自动机)
题目大意: 给你一个字符串,求其中回文子串的长度*出现次数的最大值 明明是PAM裸题我干嘛要用SAM做 回文子串有一个神奇的性质,一个字符串本质不同的回文子串个数是$O(n)$级别的 用$manach ...
- HYSBZ 3676 回文串 (回文树)
3676: [Apio2014]回文串 Time Limit: 20 Sec Memory Limit: 128 MB Submit: 1680 Solved: 707 [Submit][Stat ...
- 【bzoj3676】[Apio2014]回文串 —— 回文自动机的学习
写题遇上一棘手的题,[Apio2014]回文串,一眼看过后缀数组+Manacher.然后就码码码...过是过了,然后看一下[Status],怎么慢这么多,不服..然后就搜了一下,发现一种新东西——回文 ...
随机推荐
- 前端&后端程序员必备的Linux基础知识
一 从认识操作系统开始 1.1 操作系统简介 我通过以下四点介绍什么操作系统: 操作系统(Operation System,简称OS)是管理计算机硬件与软件资源的程序,是计算机系统的内核与基石: 操作 ...
- 【JDK】JDK7与JDK8环境共存与切换:先安装jdk7,配置好环境变量后再安装jdk8
1.先安装JDK7 下载jdk-7u79-windows-i586.exe,安装后配置好环境变量JAVA_HOME,CLASSPATH,PATH java -version javac 指令都正常 2 ...
- face_recognition 人脸识别报错
[root@localhost examples]# python facerec_from_video_file.py RuntimeError: module compiled against A ...
- [Leetcode Week16]Insertion Sort List
Insertion Sort List 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/insertion-sort-list/description/ ...
- 二叉查找树、平衡二叉树、红黑树、B-/B+树性能对比
转载:https://blog.csdn.net/z702143700/article/details/49079107 前言:BST.AVL.RBT.B-tree都是动态结构,查找时间基本都在O(l ...
- 64_l3
libguac-client-ssh-0.9.13-3.20170521git6d2cfda...> 23-May-2017 09:58 64570 libguac-client-ssh-0.9 ...
- 非 GUI 模式运行 JMeter 压力测试
非 GUI 模式,即命令行模式,运行 JMeter 测试脚本能够大大缩减所需要的系统资源. 使用命令:jmeter -n -t <testplan filename> -l <lis ...
- pycaffe使用.solverstate文件继续训练
import caffe solver_file = "solver.prototxt" solverstate = "xx.solverstate" caff ...
- 12-7 NSDictionary
原文:http://rypress.com/tutorials/objective-c/data-types/nsdictionary NSDictionary 如同NSSet,NSDictionar ...
- Linux Python apache的cgi配置
一.找到安装Apache的目录/usr/local/apache2/conf,并对httpd.conf配置文件进行修改 1.加载cgi模块 去掉注释: LoadModule cgid_module m ...