[SHOI 2011]双倍回文
Description
记一个字符串为 \(X\) ,它的倒置为 \(X^R\) 。现在给你一个长度为 \(n\) 的字符串 \(S\) ,询问其最长的形同 \(XX^RXX^R\) 的子串长度为多少。
\(1\leq n\leq 500000\)
Solution
显然求回文之类的东西还是要 \(manacher\) 的。
假设我们求出了 \(manacher\) 的 \(len\) 数组,考虑怎么去用这个 \(len\) 。
值得肯定的是我们可以用 \(len\) 来刻画“双倍回文”。对于双倍回文的中点 \(x\) ,考虑记其第三段为 \((x,y-1)\) 。
显然我们可以去枚举 \(x\) ,然后去找离 \(x\) 最远的 \(y\) 。
我们可以用不等式 \(\begin{cases} \begin{aligned} x &\geq y-len_y \\ y &\leq x+\frac{len_x}{2} \end{aligned} \end{cases}\) 来刻画一对 \((x,y)\) 。
现在就变成了枚举 \(x\) 然后去找一个满足上述情况对应的最大的 \(y\) 。
排序+ \(set\) 维护即可。
Code
//It is made by Awson on 2018.3.16
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 500000;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, loc, len[(N<<1)+5], f[N+5];
char s[N+5], ch[(N<<1)+5];
struct tt {
int id, x;
bool operator < (const tt &b) const {return id-x < b.id-b.x; }
}a[N+5];
set<int>S;
void work() {
read(n); scanf("%s", s+1);
for (int i = 1; i <= n; i++) ch[++loc] = '#', ch[++loc] = s[i];
ch[++loc] = '#', ch[++loc] = '$';
int mx = 0, po = 0;
for (int i = 1; i <= loc; i++) {
if (mx > i) len[i] = Min(len[(po<<1)-i], mx-i);
else len[i] = 1;
while (ch[i+len[i]] == ch[i-len[i]]) ++len[i];
if (i+len[i] > mx) mx = i+len[i], po = i;
}
for (int i = 1; i <= n; i++) f[i] = a[i].x = ((len[(i<<1)+1]-1)>>1), a[i].id = i;
sort(a+1, a+n+1); loc = 1; int ans = 0;
for (int i = 1; i <= n; i++) {
while (loc <= n && a[loc].id-a[loc].x <= i) S.insert(a[loc].id), ++loc;
set<int>::iterator it = S.upper_bound(i+(f[i]>>1));
if (it == S.begin()) continue;
int t = *(--it)-i; ans = Max(ans, t);
}
writeln(ans<<2);
}
int main() {
work(); return 0;
}
[SHOI 2011]双倍回文的更多相关文章
- 「BZOJ 2342」「SHOI 2011」双倍回文「Manacher」
题意 记\(s_R\)为\(s\)翻转后的串,求一个串最长的形如\(ss_Rss_R\)的子串长度 题解 这有一个复杂度明显\(O(n)\)的做法,思路来自网上某篇博客 一个双倍回文串肯定当且仅当本身 ...
- 【BZOJ-2342】双倍回文 Manacher + 并查集
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1799 Solved: 671[Submit][Statu ...
- BZOJ 2342 双倍回文(manacher算法)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2342 题意:定义双倍回文串为:串的长度为4的倍数且串的前一半.后一半.串本身均是回文的. ...
- BZOJ2342: [Shoi2011]双倍回文
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 923 Solved: 317[Submit][Status ...
- 【BZOJ2342】双倍回文(回文树)
[BZOJ2342]双倍回文(回文树) 题面 BZOJ 题解 构建出回文树之后 在\(fail\)树上进行\(dp\) 如果一个点代表的回文串长度为\(4\)的倍数 并且存在长度为它的一半的回文后缀 ...
- [SHOI2011]双倍回文
Description Input 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. Output 输出文件只有一行,即:输入数据中字符串的最长 ...
- BZOJ2342[Shoi2011]双倍回文——回文自动机
题目描述 输入 输入分为两行,第一行为一个整数,表示字符串的长度,第二行有个连续的小写的英文字符,表示字符串的内容. 输出 输出文件只有一行,即:输入数据中字符串的最长双倍回文子串的长度,如果双倍回文 ...
- bzoj千题计划306:bzoj2342: [Shoi2011]双倍回文 (回文自动机)
https://www.lydsy.com/JudgeOnline/problem.php?id=2342 解法一: 对原串构建回文自动机 抽离fail树,从根开始dfs 设len[x]表示节点x表示 ...
- 2018.06.30 BZOJ 2342: [Shoi2011]双倍回文(manacher)
2342: [Shoi2011]双倍回文 Time Limit: 10 Sec Memory Limit: 128 MB Description Input 输入分为两行,第一行为一个整数,表示字符串 ...
随机推荐
- geotrellis使用(三十六)瓦片入库更新图层
前言 Geotrellis 是针对大数据量栅格数据进行分布式空间计算的框架,这一点毋庸置疑,并且无论采取何种操作,其实都是先将大块的数据切割成一定大小的小数据(专业术语为瓦片),这是分治的思想,也是分 ...
- swift textview禁止用户使用复制粘贴
//自定义一个TextView class Own_TextView: UITextView { override func caretRect(for position: UITextPositio ...
- windows下apache报os 10048错误
在apache的bin目录下运行httpd -k install,报错os10048 (错误信息是跟443端口有关),网上的答案说的是改掉httpd.conf里的默认端口或者关闭占用端口的进程,默认端 ...
- C语言博客作业—一二维数组
一.PTA实验作业 题目1:7-2 求整数序列中出现次数最多的数 1. 本题PTA提交列表 2. 设计思路 Begin 输入整数个数N 定义数组a[N] 输入数组a for(i 0 to N-1){/ ...
- PHP、Java、Python、C、C++ 这几种编程语言都各有什么特点或优点
PHP.Java.Python.C.C++ 这几种编程语言都各有什么特点或优点 汇编: C: Java: C#: PHP: Python: Go: Haskell: Lisp: C++: &l ...
- ctf变量覆盖漏洞:
1.变量覆盖: ①:针对extract函数的变量覆盖漏洞: <?php @error_reporting(E_ALL^E_NOTICE); require('config.php'); if($ ...
- LeetCode & Q122-Best Time to Buy and Sell Stock II-Easy
Description: Say you have an array for which the ith element is the price of a given stock on day i. ...
- 新概念英语(1-43)Hurry up!
新概念英语(1-43)Hurry up! How do you know Sam doesn't make the tea very often? A:Can you make the tea, Sa ...
- 新概念英语(1-67)The weekend
新概念英语(1-67)The weekend What are the Johnsons going to do at the weekend? A:Hello. Were you at the bu ...
- EasyUI中datagrid的基本用法
EasyUI中datagrid是最常用的一个控件了,现在整理一下datagrid的基本语法,先展示下页面效果吧,如下图