题意

给定一个字符串 \(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的更多相关文章

  1. 【HDU 5030】Rabbit's String (二分+后缀数组)

    Rabbit's String Problem Description Long long ago, there lived a lot of rabbits in the forest. One d ...

  2. hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)

    Rabbit's String Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  3. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  4. 【POJ 3415】Common Substrings 长度不小于k的公共子串的个数

    长度不小于k的公共子串的个数,论文里有题解,卡了一上午,因为sum没开long long!!! 没开long long毁一生again--- 以后应该早看POJ里的Discuss啊QAQ #inclu ...

  5. POJ-Common Substrings(后缀数组-长度不小于 k 的公共子串的个数)

    题意: 长度不小于 k 的公共子串的个数 分析: 基本思路是计算 A 的所有后缀和 B 的所有后缀之间的最长公共前缀的长度,把最长公共前缀长度不小于 k 的部分全部加起来. 先将两个字符串连起来,中间 ...

  6. POJ 3415 Common Substrings(长度不小于K的公共子串的个数+后缀数组+height数组分组思想+单调栈)

    http://poj.org/problem?id=3415 题意:求长度不小于K的公共子串的个数. 思路:好题!!!拉丁字母让我Wa了好久!!单调栈又让我理解了好久!!太弱啊!! 最简单的就是暴力枚 ...

  7. POJ 3415 Common Substrings 【长度不小于 K 的公共子串的个数】

    传送门:http://poj.org/problem?id=3415 题意:给定两个串,求长度不小于 k 的公共子串的个数 解题思路: 常用技巧,通过在中间添加特殊标记符连接两个串,把两个串的问题转换 ...

  8. Common Substrings POJ - 3415(长度不小于k的公共子串的个数)

    题意: 给定两个字符串A 和 B, 求长度不小于 k 的公共子串的个数(可以相同) 分两部分求和sa[i-1] > len1  sa[i] < len1  和  sa[i-1] < ...

  9. 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 ...

  10. CSU-1632 Repeated Substrings (后缀数组)

    Description String analysis often arises in applications from biology and chemistry, such as the stu ...

随机推荐

  1. vue二级路由跳转后外部引入js失效问题解决方案

    vue路由可以通过children嵌套,于是可以形成二级路由等等... 案例如下: routes: [ { path: '/', name: 'dy', component: dy, children ...

  2. PHP和javascript中url编码解码详解

    在实际开发中,我们可能会遇到路径编码解码的问题,下面总结了一下: PHP中: 1.urlencode(编码),urldecode(解码) $a = urlencode('http://www.baid ...

  3. 静态网页、动态网页、apache和tomcat之间区别和联系

    1.静态网页 静态网页:在网站设计中, 纯粹的HTML(标准通用标志语言下的一个应用)格式的网页通常被称为"静态网页",静态网页是标准的HTML文件,它的拓拓展名是.html或者. ...

  4. Python基础部分的疑惑解析——运算符和数据类型(5)

    运算符 最后得到数值的: 算数运算符 赋值运算符 最后得到布尔值的: 成员运算符:in   not in 逻辑运算符    and   or   没有优先级就是按顺序执行 比较运算符 数据类型 1.整 ...

  5. 【算法笔记】A1039 Course List for Student

    https://pintia.cn/problem-sets/994805342720868352/problems/994805447855292416 题意: 有N个学生,K节课.给出选择每门课的 ...

  6. P1525 关押罪犯 题解

    #include<iostream> #include<cstdio> #include<algorithm> using namespace std; //带边权 ...

  7. php正则验证邮箱、手机号、姓名、身份证、特殊符号等

    1.邮箱验证 1 $email='1515212@qq'; 2 $preg_email='/^[a-zA-Z0-9]+([-_.][a-zA-Z0-9]+)*@([a-zA-Z0-9]+[-.])+( ...

  8. 常见的错误:FTP连接时出现“227 Entering Passive Mode”

    FTP的主动模式(PORT Mode)及被动模式(Passive Mode) FTP的特殊性: 大多数的TCP服务是使用单个的连接,一般是客户向服务器的一个周知端口发起连接,然后使用这个连接进行通讯. ...

  9. 我爱Markdown (3)

    继续Markdown的常见语法, 本文将介绍: 07 - Links 链接 08 - Images 图片 09 - Blockquotes 块引用 10 - Backslash Escapes 显示保 ...

  10. 在spring中常被忽视的注解 @Primary

    在spring 中使用注解,常使用@Autowired, 默认是根据类型Type来自动注入的. 但有些特殊情况,对同一个接口,可能会有几种不同的实现类,而默认只会采取其中一种的情况下 @Primary ...