传送门


假设字符串\(B,D\)满足\(|B| \geq |D|\),那么一定会有\(B=rev(D)+T\),其中\(T\)是一个回文串。

考虑枚举回文串\(T\)的中心\(p\),找到以\(p\)为中心的最长回文串\(S[l,r]\)。值得注意的是,回文串越长一定越好,因为如果回文串不是最长的,那么可以向左右拓展一位,\(B\)串长度会\(+1\),\(D\)串长度会\(-1\),没有变化。所以我们直接Manacher求最长回文串即可。

现在我们需要求的就是最小的\(i\)满足\(rev(S[i,l-1])\)是\(S[r+1,N]\)的子串。

发现对于所有可能的\(i\)都是\(S[1,l-1]\)的后缀。子串、后缀相关的问题可以考虑后缀自动机。我们对\(rev(S)\)建立SAM,对于SAM上的每一个节点记录最小的\(endpos\),并将\(S\)的所有前缀放到SAM上匹配。那么\(S[i,l-1]\)是SAM的parent树上\(S[1,l-1]\)的祖先。在parent树上倍增就可以得到满足条件的最小的\(i\)。

对于原串和反串各做一遍就可以得到最后的答案。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
//This code is written by Itst
using namespace std; #define PII pair < int , int >
const int MAXN = 2e5 + 7;
char s[MAXN];
int ind[MAXN] , len[MAXN] , L , ans;
PII ind1 , ind2;
bool flg = 0; namespace SAM{
char S[MAXN];
int Lst[MAXN] , trans[MAXN][26] , fa[MAXN] , endpos[MAXN] , jump[MAXN][19];
int cnt = 1 , lst = 1; void insert(int l , int x){
int p = lst , t = ++cnt; lst = t;
Lst[t] = endpos[t] = l;
while(p && !trans[p][x]){trans[p][x] = t; p = fa[p];}
if(!p){fa[t] = 1; return;}
int q = trans[p][x];
if(Lst[q] == Lst[p] + 1){fa[t] = q; return;}
int k = ++cnt; endpos[k] = 1e9;
Lst[k] = Lst[p] + 1;
fa[k] = fa[q]; fa[q] = fa[t] = k;
memcpy(trans[k] , trans[q] , sizeof(trans[q]));
while(trans[p][x] == q){trans[p][x] = k; p = fa[p];}
} vector < int > ch[MAXN]; void dfs(int x){
for(int i = 1 ; jump[x][i - 1] ; ++i)
jump[x][i] = jump[jump[x][i - 1]][i - 1];
for(auto t : ch[x]){jump[t][0] = x; dfs(t); endpos[x] = min(endpos[x] , endpos[t]);}
} void init(){
memcpy(S , s , sizeof(S)); reverse(S + 1 , S + L + 1);
for(int i = 1 ; i <= L ; ++i) insert(i , S[i] - 'a');
for(int i = 2 ; i <= cnt ; ++i) ch[fa[i]].push_back(i);
dfs(1);
} int query(int x , int pos){
if(endpos[x] <= pos) return x;
for(int i = 18 ; i >= 0 ; --i)
if(endpos[jump[x][i]] > pos)
x = jump[x][i];
return jump[x][0];
} void clear(){
memset(trans , 0 , sizeof(trans)); memset(Lst , 0 , sizeof(Lst));
memset(fa , 0 , sizeof(fa)); memset(endpos , 0 , sizeof(endpos));
memset(jump , 0 , sizeof(jump));
for(int i = 1 ; i <= cnt ; ++i) ch[i].clear();
cnt = lst = 1;
}
} namespace Manacher{
char S[MAXN];
int Len[MAXN]; void work(){
for(int i = 1 ; i <= L ; ++i)
S[(i << 1) - 1] = s[i];
memset(Len , 0 , sizeof(Len));
int maxR = 1 , maxI = 1;
for(int i = 1 ; i < L << 1 ; ++i){
Len[i] = min(maxR - i , Len[2 * maxI - i]);
while(i - Len[i] >= 0 && i + Len[i] <= L << 1 && S[i - Len[i]] == S[i + Len[i]])
++Len[i];
int posL = (i - Len[i] + 1) / 2 + 1 , posR = (i + Len[i] + 1) / 2 - 1;
if(posL <= posR){
int u = SAM::query(ind[posL - 1] , L - posR) , l = u == ind[posL - 1] ? len[posL - 1] : SAM::Lst[u];
if(ans < posR - posL + 1 + 2 * l){
ans = posR - posL + 1 + 2 * l;
ind1 = PII(posL - l , posR);
if(l) ind2 = PII(L - SAM::endpos[u] + 1 , L - SAM::endpos[u] + l);
else ind2 = PII(-1 , -1);
flg = 1;
}
}
if(maxR <= Len[i] + i){
maxR = Len[i] + i;
maxI = i;
}
}
}
} void work(){
flg = 0;
SAM::init(); ind[0] = 1;
for(int i = 1 ; i <= L ; ++i){
int cur = ind[i - 1] , l = len[i - 1];
while(cur && !SAM::trans[cur][s[i] - 'a']) l = SAM::Lst[cur = SAM::fa[cur]];
!cur ? ind[i] = 1 : (ind[i] = SAM::trans[cur][s[i] - 'a'] , len[i] = l + 1);
}
Manacher::work();
} int main(){
scanf("%s" , s + 1);
L = strlen(s + 1); work();
reverse(s + 1 , s + L + 1); SAM::clear(); work();
if(flg){
swap(ind2 , ind1); swap(ind1.first , ind1.second); swap(ind2.first , ind2.second);
ind1.first = L + 1 - ind1.first; ind1.second = L + 1 - ind1.second;
ind2.first = L + 1 - ind2.first; ind2.second = L + 1 - ind2.second;
}
printf("%d\n%d %d\n%d %d\n" , ans , ind1.first , ind1.second , ind2.first , ind2.second);
return 0;
}

Gym101237C The Palindrome Extraction Manacher、SAM、倍增的更多相关文章

  1. bzoj 3676 [Apio2014]回文串(Manacher+SAM)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3676 [题意] 给定一个字符串,定义一个串的权值为长度*出现次数,求最大权的回文子串. ...

  2. Codeforces Gym 100570 E. Palindrome Query Manacher

    E. Palindrome QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100570/pro ...

  3. Palindrome(Manacher)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 6183   Accepted: 2270 Descr ...

  4. Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp

    题目链接: http://codeforces.com/problemset/problem/7/D D. Palindrome Degree time limit per test1 secondm ...

  5. [BZOJ3676][APIO2014]回文串(Manacher+SAM)

    3676: [Apio2014]回文串 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 3097  Solved: 1408[Submit][Statu ...

  6. POJ3974:Palindrome(Manacher模板)

    Palindrome Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 14021   Accepted: 5374 题目链接 ...

  7. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  8. CF666E Forensic Examination SAM+倍增,线段树和并

    题面: 给你一个串S以及一个字符串数组T[1..m],q次询问,每次问S的子串S[p_l..p_r]在T[l..r]中的哪个串里的出现次数最多,并输出出现次数.如有多解输出最靠前的那一个. 分析: 第 ...

  9. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

随机推荐

  1. Javascript 对象 - 字符串对象

    字符串对象 字符串对象是JavaScript中比较常见的一种基本数据类型,他封装了一个字符串,并且提供了相应的方法.例如连接字符串.取字符串.分割字符串等.JavaScript中字符串是不可变的,原始 ...

  2. 如何查看程序所占端口号和IP

    如何查看程序所占端口号和IP 一个软件可能占用多个端口拥有多个目标IP,下面以FQ工具Lantern为例,说明端口查看方法: 1.借助第三方软件查看 如果你电脑上安装了360等优化工具,可能会自带查看 ...

  3. 区块链:创建简单的HelloWorld-WebDApp

    Node.js 一个建立在Chrome的JavaScript运行环境上的平台,用以容易地建立快速而可扩展的网络应用:下载地址:https://nodejs.org/en/ Turffle 以太坊(Et ...

  4. 我的第一个远程代码库房:建立Github仓库 心得

    一直想有一个自己的代码库,搞了两天终于搞定了,把自己的代码上传到github的愿望终于实现了,虽然仅仅是个开始. 在搭建的过程中,吸收了些知识,记录在这里,以作为分享. Git 和 Github 的区 ...

  5. [20181015]为什么是3秒.txt

    [20181015]为什么是3秒.txt --//以前测试:连接http://blog.itpub.net/267265/viewspace-2144765/=>为什么是12秒.txt.--// ...

  6. mssql sqlserver 可以存储二进制数据的字段类型详解

    转自: http://www.maomao365.com/?p=6738 摘要: 下文将从数据库的数据类型着手,剖析在sqlserver数据库中可以存储二进制数据的数据类型,如下所示: mssql s ...

  7. Android内嵌VLC实现播放网络视频,网络音频

    1.在对应模块的build.gradle文件中,添加依赖 //VlC implementation "de.mrmaffen:vlc-android-sdk:2.0.6" 2.布局 ...

  8. c/c++ 通用的(泛型)算法 generic algorithm 总览

    通用的(泛型)算法 generic algorithm 总览 特性: 1,标准库的顺序容器定义了很少的操作,比如添加,删除等. 2,问题:其实还有很多操作,比如排序,查找特定的元素,替换或删除一个特定 ...

  9. CentOS TinyProxy http(s)上网代理及置代理上网的方法

    http://blog.csdn.net/fwj380891124/article/details/42168683 http://computer.uoh.edu.cn/linux/2159.htm ...

  10. 安装完最小化 RHEL/CentOS 7 后需要做的 30 件事情(一)转载自码农网

    CentOS 是一个工业标准的 Linux 发行版,是红帽企业版 Linux 的衍生版本.你安装完后马上就可以使用,但是为了更好地使用你的系统,你需要进行一些升级.安装新的软件包.配置特定服务和应用程 ...