题意

给定一个字符串 \(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. 深度解析CNN

    [1]Deep learning简介 [2]Deep Learning训练过程 [3]Deep Learning模型之:CNN卷积神经网络推导和实现 [4]Deep Learning模型之:CNN的反 ...

  2. [原创]Laravel 的缓存源码解析

    目录 前言 使用 源码 Cache Facade CacheManager Repository Store 前言 Laravel 支持多种缓存系统, 并提供了统一的api接口. (Laravel 5 ...

  3. 【3】JMicro微服务-服务超时,重试,重试间隔

    如非授权,禁止用于商业用途,转载请注明出处作者:mynewworldyyl 接下来的内容都基于[2]JMicro微服务-Hello World做Demo 微服务中,超时和重试是一个最基本问题下面Dem ...

  4. 队列的理解和实现(一) ----- 循环队列(java实现)

    什么是队列 我们都知道栈是先进后出的一种线性表,与之相反的是,队列是一种先进先出的线性表.它只允许在表的一端进行插入,而在另一端进行删除.举个例子来说,在生活中我们买东西需要进行排队,最先排队的可以最 ...

  5. POJ 2196

    #include <iostream> using namespace std; int sum_10; int sum_12; int sum_16; int fun_10(int nu ...

  6. Java8内存结构—永久代(PermGen)和元空间(Metaspace)

    本文转载 作者:liuxiaopeng 博客地址:https://www.cnblogs.com/paddix/p/5309550.html 一.JVM 内存结构 根据 JVM 规范,JVM 内存共分 ...

  7. 解决TeamViewer无法按给定网络地址联系伙伴

    说明:这种现象一般是断网后DNS改变了,或者路由重启了没有重启网络配合导致的. 解决方法: 1.Windows: ipconfig /flushdns 2.Linux: /etc/rc.d/init. ...

  8. mvvm小论(暂记)

    广州-PC26(34627) 2:09:44 在android 线程最后用 handler = new Handler();            updateThread = new Runnabl ...

  9. xampp使用技巧及问题汇总

    1)在win7上同时装有IIS 和 xampp1.8.2 ,会出现Apache启动时,提示80端口被占用的情况(一般是iis安装之后出现的常见情况). 情况1:  xampp 在启动时会检测Apach ...

  10. feignClient中修改ribbon的配置

    1.使用@FeignClient注解发现服务 服务提供者的controller: @RestController public class StudentController { @Autowired ...