CF961F k-substrings
题意
给定一个字符串 \(S\)
求所有的 \(S[i,n-i+1]\) 的 \(border\) 长度(最长的前缀等于后缀),要求长度是奇数
\(n\le 10^6\)
Sol
首先发现每次求的串都是原串去掉前后 \(i-1\) 位得到的串
一个套路,把串翻折,又因为 \(border\) 长度可能大于一半,所以我们把串倍长后翻折
也就是翻转过来隔空插入在一起
例如:
串 \(bcabcabcabcabca\)
翻转后 \(acbacbacbacbacb\)
隔一个插入在一起 \(baccabbaccabbaccabbaccabbaccab\)
那么也就是求这个串的以某个位置的开始的最长回文串
又因为得到的这个串本身就是回文串,所以并不用翻转过来,直接求以某个位置的结束的最长回文串就好了
比如 \(baccab\) 就是 \(S[1,3]\) 和 \(S[13,15]\)
回文树就好了
注意到每次都要跳 \(fail\) 链跳到满足要求的位置,而每次都跳很耗时
如果之后跳到之前跳到过的点,就可以直接跳到之前跳到的对答案有贡献的点上
再继续跳
并查集维护一下就好了
# include <bits/stdc++.h>
# define IL inline
# define RG register
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
const int maxn(2e6 + 5);
int n, last, tot, anc[maxn], id[maxn];
int len[maxn], first[maxn], nxt[maxn], type[maxn], fa[maxn];
char s[maxn], str[maxn];
IL int Son(RG int u, RG int c){
for(RG int v = first[u]; v; v = nxt[v])
if(type[v] == c) return v;
return 0;
}
IL void Link(RG int u, RG int v, RG int c){
nxt[v] = first[u], first[u] = v, type[v] = c;
}
IL void Extend(RG int pos, RG int c){
RG int p = last;
while(s[pos - len[p] - 1] != s[pos]) p = fa[p];
if(!Son(p, c)){
RG int np = ++tot, q = fa[p];
while(s[pos - len[q] - 1] != s[pos]) q = fa[q];
len[np] = len[p] + 2, fa[np] = Son(q, c);
Link(p, np, c);
}
last = Son(p, c);
}
IL int Find(RG int x){
return anc[x] == x ? x : anc[x] = Find(anc[x]);
}
int main(){
Fill(type, -1), n = Input(), scanf(" %s", str + 1);
for(RG int t = 0, i = 1, j = n; j; ++i, --j)
s[++t] = str[i], s[++t] = str[j];
tot = 1, fa[0] = fa[1] = 1, len[1] = -1, n <<= 1, anc[0] = 1;
for(RG int i = 1; i <= n; ++i) Extend(i, s[i] - 'a'), id[i] = last;
for(RG int i = 0; i <= tot; ++i) anc[i] = i;
for(RG int i = 1, m = n >> 1, t = (m + 1) >> 1; i <= t; ++i){
RG int x = Find(id[n - ((i - 1) << 1)]);
while(x != 1 && ((len[x] >> 1) >= (m - ((i - 1) << 1)) || len[x] % 4 != 2)) x = anc[x] = Find(fa[anc[x]]);
printf("%d ", (len[x] % 4 == 2) ? (len[x] >> 1) : -1);
}
return 0;
}
CF961F k-substrings的更多相关文章
- 【HDU 5030】Rabbit's String (二分+后缀数组)
Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...
- hdu 5030 Rabbit's String(后缀数组&二分法)
Rabbit's String Time Limit: 40000/20000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- django模型操作
Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表
- 【POJ 3415】Common Substrings 长度不小于k的公共子串的个数
长度不小于k的公共子串的个数,论文里有题解,卡了一上午,因为sum没开long long!!! 没开long long毁一生again--- 以后应该早看POJ里的Discuss啊QAQ #inclu ...
- POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)
题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...
- POJ 3415 Common Substrings(长度不小于K的公共子串的个数+后缀数组+height数组分组思想+单调栈)
http://poj.org/problem?id=3415 题意:求长度不小于K的公共子串的个数. 思路:好题!!!拉丁字母让我Wa了好久!!单调栈又让我理解了好久!!太弱啊!! 最简单的就是暴力枚 ...
- POJ 3415 Common Substrings 【长度不小于 K 的公共子串的个数】
传送门:http://poj.org/problem?id=3415 题意:给定两个串,求长度不小于 k 的公共子串的个数 解题思路: 常用技巧,通过在中间添加特殊标记符连接两个串,把两个串的问题转换 ...
- Common Substrings POJ - 3415(长度不小于k的公共子串的个数)
题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1 sa[i] < len1 和 sa[i-1] < ...
- POJ - 3415 Common Substrings(后缀数组求长度不小于 k 的公共子串的个数+单调栈优化)
Description A substring of a string T is defined as: T( i, k)= TiTi+1... Ti+k-1, 1≤ i≤ i+k-1≤| T|. G ...
- CSU-1632 Repeated Substrings (后缀数组)
Description String analysis often arises in applications from biology and chemistry, such as the stu ...
随机推荐
- WebStorm 简单部署服务器进行测试操作
WebStorm 简单部署服务器对外发布接口 第一步: 查看webstorm防火墙是否允许链接,控制面板-->防火墙-->高级设置 入站规则-->webstrom是否允许链接 ,双击 ...
- Oracle ltrim() rtrim() 函数详细用法
今天在论坛里看了一篇帖子,讨论ltrim() 函数的详细用法,下面我借几个高手的回答总结一下: 先看几个实例: SQL> select ltrim('109224323','109') from ...
- jquery scrollTop()与scrollLeft()
1.scrollLeft() scrollLeft() 方法设置或返回被选元素的水平滚动条位置. 提示:当滚动条位于最左侧时,位置是 0. 当用于返回位置时:该方法返回第一个匹配元素的滚动条的水平位置 ...
- Python文件读取和数据处理
一.python文件读取 1.基本操作 读取文件信息时要注意文件编码,文件编码有UFT-8.ASCII或UTF-16等. 不过在python中最为常用的是UTF-8,所以如果不特别说明就默认UTF-8 ...
- python学习,day3:函数式编程,局部变量和全局变量
# coding=utf-8 # Author: RyAn Bi school = 'THU' #全局变量 def change_name(name): global age #在函数中,用globa ...
- Service Discovery protocol(SDP)
locating services provided by Volume 3 , Part C , section 8 2.1sdp client-server architecture 2.2 se ...
- Linux - iptables firewalld
目录 iptables firewalld iptables 1.iptables 的基本使用 启动: service start iptabls 关闭: service stopiptabls 查看 ...
- Centos7安装python3.7.1并与python2共存
转自:http://www.cnblogs.com/JahanGu/p/7452527.html参考:https://www.jb51.net/article/104326.htm 1. 备份原来的p ...
- 【ORACLE】Bulk Processing with BULK COLLECT and FORALL
https://orablogspot.blogspot.com/2014/09/ https://blogs.oracle.com/oraclemagazine/bulk-processing-wi ...
- lrzsz
新搞的云服务器用SecureCRT不支持上传和下载,没有找到rz命令.记录一下如何安装rz/sz命令的方法. 一.工具说明 在SecureCRT这样的ssh登录软件里, 通过在Linux界面里输入rz ...