【UOJ #103】【APIO 2014】Palindromes
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的更多相关文章
- 【UOJ #104】【APIO 2014】Split the sequence
http://uoj.ac/problem/104 此题的重点是答案只与切割的最终形态有关,与切割顺序无关. 设\(f(i,j)\)表示前\(i\)个元素切成\(j\)个能产生的最大贡献. \(f(i ...
- uoj 41 【清华集训2014】矩阵变换 婚姻稳定问题
[清华集训2014]矩阵变换 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/41 Description 给出 ...
- AC日记——【清华集训2014】奇数国 uoj 38
#38. [清华集训2014]奇数国 思路: 题目中的number与product不想冲: 即为number与product互素: 所以,求phi(product)即可: 除一个数等同于在模的意义下乘 ...
- 【UOJ】67 新年的毒瘤 &【BZOJ】1123 BLO
[UOJ 67] 题目链接: 传送门 题解: 第一眼很懵逼……这什么鬼. 思考什么点复合条件……(o(>﹏<)o 1.树,也就是说还剩n-2条边,等价于要删去一个度数为m-n+2的点. 2 ...
- 【UOJ#236】[IOI2016]railroad(欧拉回路,最小生成树)
[UOJ#236][IOI2016]railroad(欧拉回路,最小生成树) 题面 UOJ 题解 把速度看成点,给定的路段看成边,那么现在就有了若干边,然后现在要补上若干边,以及一条\([inf,\) ...
- 【UOJ#177】欧拉回路
[UOJ#177]欧拉回路 题面 UOJ 题解 首先图不连通就没啥好搞的了. 对于无向图而言,每个点度数为偶数. 对于有向图而言,每个点入度等于出度. 然后就是一本通上有的做法,直接\(dfs\)一遍 ...
- 【UOJ#311】【UNR #2】积劳成疾(动态规划)
[UOJ#311][UNR #2]积劳成疾(动态规划) UOJ Solution 考虑最大值分治解决问题.每次枚举最大值所在的位置,强制不能跨过最大值,左右此时不会影响,可以分开考虑. 那么设\(f[ ...
- 【UOJ#450】【集训队作业2018】复读机(生成函数,单位根反演)
[UOJ#450][集训队作业2018]复读机(生成函数,单位根反演) 题面 UOJ 题解 似乎是\(\mbox{Anson}\)爷的题. \(d=1\)的时候,随便怎么都行,答案就是\(k^n\). ...
- 【UOJ#246】套路(动态规划)
[UOJ#246]套路(动态规划) 题面 UOJ 题解 假如答案的选择的区间长度很小,我们可以做一个暴力\(dp\)计算\(s(l,r)\),即\(s(l,r)=min(s(l+1,r),s(l,r- ...
随机推荐
- LintCode 190: Next Permutation
LintCode 190: Next Permutation 题目描述 给定一个若干整数的排列,给出按正数大小进行字典序从小到大排序后的下一个排列. 如果没有下一个排列,则输出字典序最小的序列. 样例 ...
- [转]C语言指针详解(经典,非常详细)
博文地址:https://blog.csdn.net/constantin_/article/details/79575638 写得很好啊! 这里写一下笔记好了 int p; //这是一个普通的整型变 ...
- htmlunit爬虫工具使用--模拟浏览器发送请求,获取JS动态生成的页面内容
Htmlunit是一款模拟浏览抓取页面内容的java框架,具有js解析引擎(rhino),可以解析页面的js脚本,得到完整的页面内容,特殊适合于这种非完整页面的站点抓取. 下载地址: https:// ...
- bootstrap分页查询传递中文参数到后台(get方式提交)
<!--分页 --> <div style="width: 380px; margin: 0 auto; margin-top: 50px;"> <u ...
- Android :ExpandableListActivity
http://developer.android.com/reference/android/app/ExpandableListActivity.html# public class Expanda ...
- iTextSharp操作pdf之pdfCreater
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Machine Learning系列--深入理解拉格朗日乘子法(Lagrange Multiplier) 和KKT条件
在求取有约束条件的优化问题时,拉格朗日乘子法(Lagrange Multiplier) 和KKT条件是非常重要的两个求取方法,对于等式约束的优化问题,可以应用拉格朗日乘子法去求取最优值:如果含有不等式 ...
- 头像截图上传三种方式之一(一个简单易用的flash插件)(asp.net版本)
flash中有版权声明,不适合商业开发.这是官网地址:http://www.hdfu.net/ 本文参考了http://blog.csdn.net/yafei450225664/article/det ...
- 详述Linux配置静态IP、设置DNS和主机名(一)
Linux配置静态IP.设置DNS和主机名首先要找到配置文件,这是在Linux系统下进行工作的必须知道工作方式.后面一步步的跟着这个范例来进行配置相信你最终也会完成Linux配置静态IP.设置DNS和 ...
- xcode没有ios7的模拟器
xcode7 目前只支持 ios8盒和iOS9的模拟器如果是Yosemite系统,下载xcode7和xcode6.4,两个版本可以共存,然后再下载iOS7默契你如果是EI Caption系统,网上说E ...