## [$>Codeforces \space 30\ E. Tricky\ and\ Cleve\ Password

题目大意 : 给出一个串 \(S\),让你找出 \(A, B, C\) 三个串,满足 \(C\) 是一个后缀, \(A + B + C\)是一个回文串,\(B\) 是一个长度为奇数的回文串,且 \(A, C\) 可以为空,并最大化 \(|A| + |B| + |C|\)

\(1 \leq |S| \leq 10^5\)

解题思路 :

考虑 \(B\) 是不能为空的,不妨先用 \(Manacher\) 跑一边,然后枚举 \(B\) 的回文中心

那么问题转化为,在 \(B\) 之前的某个子串和在 \(B\) 之后的后缀的反串最长匹配长度

可以 \(KMP\) 出对于每一个 \(i\),\(mx_i\) 表示以 \(i\) 结尾的前缀最长匹配了多长的反串后缀

那么对于回文中心 \(i\), 设其回文半径为 \(r_i\),其作为 \(B\) 串回文中心的答案就是 \(2(p_i+\min(mt[i-r_i], n - i - r_i + 1) - 1\)

所以统计枚举每一个回文中心,\(O(1)\) 统计答案即可,总复杂度是 \(O(n)\),更多内容可以参考 2014年集训队论文 《浅谈回文子串问题 》—— 徐毅

/*program by mangoyang*/
#include<bits/stdc++.h>
#define inf (0x7f7f7f7f)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
} const int N = 500005;
char s[N];
int pr[N], n;
struct Match{ int l, r, len; } mt[N];
struct Answer{ int l, r, p, su, val; } ans; namespace Manacher{
char t[N]; int r[N], len;
inline void init(char *s){
for(int i = 1; i <= n; i++)
t[++len] = '#', t[++len] = s[i]; t[++len] = '#';
}
inline void realmain(){
int mx = 0, p = 0;
for(int i = 1; i <= len; i++){
if(mx > i) r[i] = Min(r[2*p-i], mx - i); else r[i] = 1;
while(r[i] < i && i + r[i] - 1 <= len && t[i-r[i]] == t[i+r[i]]) ++r[i];
if(i + r[i] - 1 > mx) mx = i + r[i] - 1, p = i;
}
for(int i = 2; i <= len; i += 2) pr[i/2] = r[i] / 2;
}
}
namespace Kmp{
char t[N]; int nxt[N];
inline void init(char *s){
for(int i = 1; i <= n; i++) t[i] = s[n-i+1];
nxt[0] = -1;
for(int i = 1, j = -1; i <= n; nxt[i++] = ++j)
while(t[j+1] != t[i] && ~j) j = nxt[j];
}
inline void realmain(char *s){
for(int i = 1, j = 0; i <= n; i++){
while(t[j+1] != s[i] && ~j) j = nxt[j];
mt[i].len = ++j, mt[i].r = i, mt[i].l = i - j + 1;
}
for(int i = 1; i <= n; i++)
if(mt[i-1].len > mt[i].len) mt[i] = mt[i-1];
}
}
int main(){
scanf("%s", s + 1), n = strlen(s + 1);
Kmp::init(s), Kmp::realmain(s);
Manacher::init(s), Manacher::realmain();
for(int i = 1; i <= n; i++){
int pos = i - pr[i], suf = min(mt[pos].len, n - i - pr[i] + 1);
int res = 2 * (pr[i] + suf) - 1;
if(res > ans.val) ans = (Answer){mt[pos].l, mt[pos].r, i, suf, res};
}
int l1 = ans.l, r1 = ans.r;
int l2 = ans.p - pr[ans.p] + 1, r2 = ans.p + pr[ans.p] - 1;
int l3 = n - ans.su + 1, r3 = n, tot = 3;
r1 = min(r1, l2 - 1), l3 = max(l3, r2 + 1);
if(r1 < l1 || !l1 || !r1) tot--;
if(l3 > r3 || l3 > n || r3 > n) tot--;
cout << tot << endl;
if(l1 <= r1 && l1 && r1) cout << l1 << " " << r1 - l1 + 1 << endl;
cout << l2 << " " << r2 - l2 + 1 << endl;
if(l3 <= r3 && l3 <= n && r3 <= n) cout << l3 << " " << r3 - l3 + 1 << endl;
return 0;
}

Codeforces 30 E. Tricky and Cleve Password的更多相关文章

  1. Codeforces 196 E. Tricky and Cleve Password

    \(>Codeforces \space 196\ E. Tricky\ and\ Cleve\ Password<\) 题目大意 : 给出一个有 \(n\) 个结点,\(m\) 条边的连 ...

  2. 算法训练 Tricky and Clever Password

     算法训练 Tricky and Clever Password   时间限制:2.0s   内存限制:256.0MB      问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人 ...

  3. 算法笔记_055:蓝桥杯练习 Tricky and Clever Password (Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人数据并不是完全安全地隐蔽.对他来说是,这不可接受的.因此,他发明了一种密码,好 ...

  4. 【Codeforces 429D】 Tricky Function

    [题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...

  5. 【codeforces 429D】Tricky Function

    [题目链接]:http://codeforces.com/problemset/problem/429/D [题意] 给你n个数字; 让你求出一段区间[l,r] 使得 (r−l)2+(∑rl+1a[i ...

  6. [CF30E]Tricky and Clever Password(KMP+manacher)

    首先枚举回文中心,然后显然中心两边要尽量扩展作为middle,这个用manacher实现. 然后注意到suffix的结尾位置是固定的(串尾),那么预处理出以每个位置结尾的串与原串后缀至多能匹配多长,然 ...

  7. CF30E. Tricky and Clever Password

    被你谷翻译诈骗了兄弟. 不过下次可以拿去诈骗其他人. 考虑枚举B,显然结论有B作为回文串越长越好,这个可以使用manacher,或者直接二分hash. 然后考虑翻转末尾串,然后记录其匹配到第 \(i\ ...

  8. CentOS6.5 安装mysql5.6.30

    1.下载解压由于系统会自带mysql5.1版本的数据库,需要卸载.[root@localhost src]# yum remove -y mysql-libs[root@localhost src]# ...

  9. docker登录报错Error response from daemon: Get https://192.168.30.10/v1/users/: dial tcp 192.168.30.10:443: connect: connection refused

    背景描述: 登录docker报错: [root@localhost sysconfig]# docker login 192.168.30.10 Username (newcs06): newcs06 ...

随机推荐

  1. PHP做文件限速下载

    <?php include("DBDA.class.php"); $db = new DBDA(); $bs = $_SERVER["QUERY_STRING&qu ...

  2. HDU 2516 取石子游戏 (找规律)

    题目链接 Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任意多个,但不能全部取完.以后每次取的石子数不能超过上次取子数的2倍.取完者胜.先取者负输出" ...

  3. LOW逼三人组(二)----选择排序算法

    选择排序思路: 算法程序: def cal_time(func): # 装饰器 ,用来检测算法所执行的时间 def wrapper(*args,**kwargs): t1=time.time() re ...

  4. Redis 启动警告解决【转】

    [root@centos224]# service redisd start :M Nov :: (it was originally set to ). _._ _.-``__ ''-._ _.-` ...

  5. 选中一行并且选中该行的radio

    $("tr").bind("click",function(){ $("input:radio").attr("checked&q ...

  6. Ubuntu 下查看已安装的软件

    Ubuntu 下如何查看已安装的软件 1.查看安装的所有软件 dpkg -l 例如: dpkg -l | grep ftp 2.查看软件安装的路径 dpkg -L | grep ftp 也可以用 wh ...

  7. c++鼠标点点,获取坐标值,放入到txt文件中

    // oj3.cpp : Defines the entry point for the console application.// #include "stdafx.h"#in ...

  8. golang类型转换小总结

    1. int <--> string 1.1. int --> string str := strconv.Itoa(intVal) 当然,整数转换成字符串还有其他方法,比如 fmt ...

  9. Adding Completion to (interactive)

      Adding Completion to (interactive) Author: Tubo Question: Is there any way to add my own completio ...

  10. Linux 用户篇——用户管理命令之id、whoami、su、chage

    一.浅谈id.whoami.su.chage 本篇是续写上一篇<Linux 用户篇——用户管理命令之useradd.passwd.userdel.usermod>. (1)id命令 命令格 ...