http://uoj.ac/problem/103

由manacher得:本质不同的回文串只有\(O(n)\)个。

用manacher求出所有本质不同的回文串,对每个本质不同的回文串,在后缀自动机的parent树上倍增求一下它出现了多少次,更新答案。

时间复杂度\(O(n\log n)\)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll; const int N = 300003; char s[N], r[N << 1];
int n, tot = 0, top = -1, endtot = 0; struct State {
State *par, *go[26], *fa[21];
int val, sum;
} *root, *last, pool[N << 2], *endpos[N]; State *newState(int num) {
State *t = &pool[++top];
t->val = num;
t->par = 0; t->sum = 0;
memset(t->go, 0, sizeof(t->go));
return t;
} void extend(int w) {
State *p = last;
State *np = newState(p->val + 1);
while (p && p->go[w] == 0)
p->go[w] = np, p = p->par;
if (p == 0) np->par = root;
else {
State *q = p->go[w];
if (q->val == p->val + 1) np->par = q;
else {
State *nq = newState(p->val + 1);
memcpy(nq->go, q->go, sizeof(q->go));
nq->par = q->par; q->par = np->par = nq;
while (p && p->go[w] == q)
p->go[w] = nq, p = p->par;
}
}
endpos[++endtot] = last = np;
last->sum = 1;
} int len[N << 1], id[N << 2]; ll cal(int t, int l) {
State *p = endpos[t];
for (int i = 20; i >= 0; --i)
if (p->fa[i]->val >= l)
p = p->fa[i];
return 1ll * p->sum * l;
} bool cmp(int x, int y) {return pool[x].val < pool[y].val;} int main() {
scanf("%s", s + 1);
n = strlen(s + 1);
root = last = newState(0);
for (int i = 1; i <= n; ++i)
extend(s[i] - 'a'); r[0] = '#';
for (int i = 1; i <= n; ++i) {
r[++tot] = '$';
r[++tot] = s[i];
}
r[++tot] = '$';
r[++tot] = '&'; root->par = &pool[top + 1]; root->par->fa[0] = root->par; root->par->val = -1;
for (int i = 0; i <= top; ++i) pool[i].fa[0] = pool[i].par;
for (int j = 1; j <= 20; ++j)
for (int i = 0; i <= top + 1; ++i)
pool[i].fa[j] = pool[i].fa[j - 1]->fa[j - 1]; for (int i = 0; i <= top; ++i) id[i] = i;
stable_sort(id, id + top + 1, cmp);
for (int i = top; i >= 0; --i)
pool[id[i]].par->sum += pool[id[i]].sum; ll ans = 0;
int cur = 0, pos; len[0] = 1;
for (int i = 1; i < tot; ++i) {
int &now = len[i]; pos = (cur << 1) - i; now = 0;
now = min(len[pos], cur + len[cur] - i); now = max(now, 0);
while (r[i - now] == r[i + now]) {
++now;
if ((i + now - 1) & 1) continue;
ans = max(ans, cal((i + now - 1) >> 1, now));
}
if (i + now > cur + len[cur]) cur = i;
} printf("%lld\n", ans); return 0;
}

【UOJ #103】【APIO 2014】Palindromes的更多相关文章

  1. 【UOJ #104】【APIO 2014】Split the sequence

    http://uoj.ac/problem/104 此题的重点是答案只与切割的最终形态有关,与切割顺序无关. 设\(f(i,j)\)表示前\(i\)个元素切成\(j\)个能产生的最大贡献. \(f(i ...

  2. uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题

    [清华集训2014]矩阵变换 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/41 Description 给出 ...

  3. AC日记——【清华集训2014】奇数国 uoj 38

    #38. [清华集训2014]奇数国 思路: 题目中的number与product不想冲: 即为number与product互素: 所以,求phi(product)即可: 除一个数等同于在模的意义下乘 ...

  4. 【UOJ】67 新年的毒瘤 &【BZOJ】1123 BLO

    [UOJ 67] 题目链接: 传送门 题解: 第一眼很懵逼……这什么鬼. 思考什么点复合条件……(o(>﹏<)o 1.树,也就是说还剩n-2条边,等价于要删去一个度数为m-n+2的点. 2 ...

  5. 【UOJ#236】[IOI2016]railroad(欧拉回路,最小生成树)

    [UOJ#236][IOI2016]railroad(欧拉回路,最小生成树) 题面 UOJ 题解 把速度看成点,给定的路段看成边,那么现在就有了若干边,然后现在要补上若干边,以及一条\([inf,\) ...

  6. 【UOJ#177】欧拉回路

    [UOJ#177]欧拉回路 题面 UOJ 题解 首先图不连通就没啥好搞的了. 对于无向图而言,每个点度数为偶数. 对于有向图而言,每个点入度等于出度. 然后就是一本通上有的做法,直接\(dfs\)一遍 ...

  7. 【UOJ#311】【UNR #2】积劳成疾(动态规划)

    [UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...

  8. 【UOJ#450】【集训队作业2018】复读机(生成函数,单位根反演)

    [UOJ#450][集训队作业2018]复读机(生成函数,单位根反演) 题面 UOJ 题解 似乎是\(\mbox{Anson}\)爷的题. \(d=1\)的时候,随便怎么都行,答案就是\(k^n\). ...

  9. 【UOJ#246】套路(动态规划)

    [UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...

随机推荐

  1. 【Ural】1519. Formula 1 插头DP

    [题目]1519. Formula 1 [题意]给定n*m个方格图,有一些障碍格,求非障碍格的哈密顿回路数量.n,m<=12. [算法]插头DP [题解]<基于连通性状态压缩的动态规划问题 ...

  2. 关于HttpWebRequest发生服务器协议冲突的解决办法

    WinForm下的app.config文件中添加: <system.net>    <settings>      <httpWebRequest useUnsafeHe ...

  3. 工作中常用的Git操作--------(一)

    今天主要记录一下平常工作当中使用的git操作: 1.git的安装这里省略: 2.git的操作指令: 在项目开发中,经常是拉去经理已经搭建好的一个项目,也就是给我们一个git地址.比如:http://g ...

  4. JS中短路运算符&&和||

    在JS函数中我们经常会使用到短路运算符,主要是逻辑与(&&) 和 逻辑或(||) 1.逻辑与 && 的运算方式 var a = 5 && 6; cons ...

  5. PyCharm 自定义文件和代码模板

    PyCharm提供了文件和代码模板功能,可以利用此模板来快捷新建代码或文件.比如在PyCharm中新建一个html文件,新的文件并不是空的,而是会自动填充了一些基础的必备的内容,就像这样: <! ...

  6. Mac Sublime Vim模式 方向键无法长按

    终端输入 sublime2: defaults write com.sublimetext.2 ApplePressAndHoldEnabled -bool false sublime3: defau ...

  7. 使用 Xtrabackup 在线对MySQL做主从复制【转】

    1. 说明 1.1 xtrabackup mysqldump对于导出10G以下的数据库或几个表,还是适用的,而且更快捷.一旦数据量达到100-500G,无论是对原库的压力还是导出的性能,mysqldu ...

  8. 56.Merge Intervals---贪心---《编程之美》2.19区间重合判断

    题目链接:https://leetcode.com/problems/merge-intervals/description/ 题目大意:给出一串list,里面装interval类,这个类里有star ...

  9. Petrozavodsk Summer Training Camp 2017

    Petrozavodsk Summer Training Camp 2017 Problem A. Connectivity 题目描述:有\(n\)个点,现不断地加边.每条边有一种颜色,如果一个点对\ ...

  10. php 全文搜索解决方法

    全套解决方案 xunsearch 一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-dev ...