3676: [Apio2014]回文串


Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 3396  Solved: 1568
[Submit][Status][Discuss]

Description


考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出 
现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最 
大出现值。

Input


输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。

Output


输出一个整数,为逝查回文子串的最大出现值。

Sample Input


【样例输入l】

abacaba 

【样例输入2]

www 

Sample Output


【样例输出l】

 

【样例输出2]

 

HINT


一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。

在第一个样例中,回文子串有7个:a,b,c,aba,aca,bacab,abacaba,其中:

● a出现4次,其出现值为4:1:1=4

● b出现2次,其出现值为2:1:1=2

● c出现1次,其出现值为l:1:l=l

● aba出现2次,其出现值为2:1:3=6

● aca出现1次,其出现值为1=1:3=3

●bacab出现1次,其出现值为1:1:5=5

● abacaba出现1次,其出现值为1:1:7=7

故最大回文子串出现值为7。

【数据规模与评分】

数据满足1≤字符串长度≤300000。

分析:


回文树眼题,但是我不会回文树QAQ

于是后缀自动机加manacher,构造出后缀自动机parent树后倍增每个点2^i能到达的点的dis值。
然后就可以做了。。。

AC代码:


# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int N = 3e5 + ;
long long ans;
int ch[N << ][];
int dis[N << ],fa[N << ],w[N << ],cnt = ,len;
int que[N << ],p[N << ],dt,Log[],g[N << ][],Id[N];
char str[N],s[N << ];
int Sam(int c,int last)
{
int u = last,cur;
while(u && !ch[u][c])que[++que[]] = u,u = fa[u];
if(!u)
{
cur = ++cnt;fa[cur] = ;
while(que[])ch[que[que[]--]][c] = cur;
}
else
{
int v = ch[u][c];
if(dis[v] == dis[u] + )
{
cur = ++cnt;fa[cur] = v;
while(que[])ch[que[que[]--]][c] = cur;
}
else
{
int av = ++cnt;dis[av] = dis[u] + ;cur = ++cnt;
while(que[])ch[que[que[]--]][c] = cur;
memcpy(ch[av],ch[v],sizeof ch[v]);
fa[av] = fa[v];fa[v] = fa[cur] = av;
while(u && ch[u][c] == v)ch[u][c] = av,u = fa[u];
}
}
dis[cur] = dis[last] + ;
return cur;
}
void Manacher()
{
s[] = '$';s[dt = ] = '#';
for(int i = ;i <= len;i++)s[++dt] = str[i],s[++dt] = '#';
int mx = ,id = ;p[] = p[] = ;
for(int i = ;i < dt;i++)
{
p[i] = min(p[ * id - i],mx - i);
while(s[i + p[i] + ] == s[i - p[i] - ])
{
p[i]++;
if((i + p[i]) & )
{
int now = Id[(i + p[i] - ) >> ],L = ((i + p[i] - ) >> ) - ((i - p[i] + ) >> ) + ;
for(int j = ;~j;j--)if(dis[g[now][j]] >= L)now = g[now][j];
ans = max(ans,1LL * L * w[now]);
}
}
if(i + p[i] > mx)
{
id = i;
mx = i + p[i];
}
}
}
void init()
{
Log[] = ;for(int i = ;i < ;i++)Log[i] = Log[i - ] << ;
for(int i = ;i <= cnt;i++)g[i][] = fa[i];//这里考场上写成了<=len,结果得了90
for(int j = ;j <= ;j++)
for(int i = ;i <= cnt;i++)
g[i][j] = g[g[i][j - ]][j - ];
}
int main()
{
scanf("%s",str + );len = strlen(str + );Id[] = ;
for(int i = ;i <= len;i++)Id[i] = Sam(str[i] - 'a',Id[i - ]),w[Id[i]]++;
for(int i = ;i <= cnt;i++)p[dis[i]]++;
for(int i = ;i <= len;i++)p[i] += p[i - ];
for(int i = cnt;i >= ;i--)que[p[dis[i]]--] = i;
for(int i = cnt;i >= ;i--)w[fa[que[i]]] += w[que[i]];
init();Manacher();
printf("%lld\n",ans);
return ;
}

[Bzoj3676][Apio2014]回文串(后缀自动机)(parent树)(倍增)的更多相关文章

  1. bzoj3676 [Apio2014]回文串 卡常+SAM+树上倍增

    bzoj3676 [Apio2014]回文串 SAM+树上倍增 链接 bzoj luogu 思路 根据manacher可以知道,每次暴力扩展才有可能出现新的回文串. 所以推出本质不同的回文串个数是O( ...

  2. BZOJ 3676 [Apio2014]回文串 (后缀自动机+manacher/回文自动机)

    题目大意: 给你一个字符串,求其中回文子串的长度*出现次数的最大值 明明是PAM裸题我干嘛要用SAM做 回文子串有一个神奇的性质,一个字符串本质不同的回文子串个数是$O(n)$级别的 用$manach ...

  3. BZOJ 3676: [Apio2014]回文串 后缀自动机 Manacher 倍增

    http://www.lydsy.com/JudgeOnline/problem.php?id=3676 过程很艰难了,第一次提交Manacher忘了更新p数组,超时,第二次是倍增的第0维直接在自动机 ...

  4. [APIO2014]回文串 后缀自动机_Manancher_倍增

    Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorithm> #include <cstr ...

  5. [模板] 回文树/回文自动机 && BZOJ3676:[Apio2014]回文串

    回文树/回文自动机 放链接: 回文树或者回文自动机,及相关例题 - F.W.Nietzsche - 博客园 状态数的线性证明 并没有看懂上面的证明,所以自己脑补了一个... 引理: 每一个回文串都是字 ...

  6. [BZOJ3676][APIO2014]回文串(Manacher+SAM)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3097  Solved: 1408[Submit][Statu ...

  7. BZOJ3676 APIO2014回文串(manacher+后缀自动机)

    由于本质不同的回文子串数量是O(n)的,考虑在对于每个回文子串在第一次找到它时对其暴力统计.可以发现manacher时若右端点移动则找到了一个新回文串.注意这样会漏掉串长为1的情况,特判一下. 现在问 ...

  8. 2018.12.15 bzoj3676: [Apio2014]回文串(后缀自动机)

    传送门 对原串建立一个后缀自动机,然后用反串在上面匹配. 如果当前匹配的区间[l,r][l,r][l,r]包裹了当前状态的endposendposendpos中的最大值,那么[l,maxpos][l, ...

  9. [bzoj3676][Apio2014]回文串——Manacher+后缀自动机+倍增

    Brief Description 一个回文串的value定义为这个回文串的长度乘以出现次数.给定一个字符串,求\(value_{max}\). Algorithm Design 我们使用Manach ...

随机推荐

  1. HTML5<picture>元素

    HTML5<picture>元素可以设置多张图片 <!DOCTYPE html><html><head><meta http-equiv=&quo ...

  2. shell脚本,计算从0+2+4+6+....100的结果是多少?

    [root@localhost wyb]# cat evenjia.sh #!/bin/bash #从0++++...100的结果 i= ` do sum=$(($sum+i)) i=$(($i+)) ...

  3. XML解析(一) DOM解析

    XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...

  4. NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY

    NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...

  5. Flux reference

    https://facebook.github.io/flux/docs/dispatcher.html#content 首先安装 npm install --save flux Dispatcher ...

  6. LeetCode(109) Convert Sorted List to Binary Search Tree

    题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height b ...

  7. Codeforces C. Sonya and Problem Wihtout a Legend(DP)

    Description Sonya was unable to think of a story for this problem, so here comes the formal descript ...

  8. git commit 含有中文的代码,提示Warnning:Your console font probably doesn't support Unicode.......

    git 提交代码是会遇到以下问题, git commit 代码时提示: Warning: Your console font probably doesn't support Unicode. If ...

  9. nw.js学习地址

    http://blog.sina.com.cn/s/blog_600e56a60102vqj2.html https://github.com/nwjs/nw.js/wiki/Manifest-For ...

  10. python基础-对象

    1. 对象:一组数据和操作数据方法的集合 >>> class Person(object): ...     def __init__(self,name): ...         ...