## [$>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. Hive ORC表的使用

    创建普通临时表: create table if not exists test_orc_tmp(   name string,   gender string,   cnt BIGINT )row ...

  2. Linux下如何打开img镜像文件

    有些镜像文件为IMG格式,在Linux如何打开呢?例如从微软dreampark下载的Windows Server 2008 R2镜像文件,使用file命令查看: $ file chs_windows_ ...

  3. windos8设置cpu数量和内存大小

    转自:http://smilejay.com/2012/03/windows_cpu_memory_setting/ Windows 8(测试版)在作为Xen Guest中的benchmark测试.我 ...

  4. Linux文件访问流程及磁盘inode和block总结

    Linux文件访问流程 inode是文件的唯一标识,文件名和inode的对应关系存放在上一级目录的block中:inode里有指向文件block的指针和文件的属性,从而通过block获得文件数据. 磁 ...

  5. java版云笔记(四)

    页面的笔记本加载完成了,接下来就是点击笔记本显示将笔记显示,同时把笔记在右边的编辑器中,同时把编辑后的笔记更新. 注:这个项目的sql文件,需求文档,需要的html文件,jar包都可以去下载,下载地址 ...

  6. (二)Spring 之IOC 详解

    第一节:spring ioc 简介 IOC(控制反转:Inversion of Control),又称作依赖注入dependency injection( DI ),是一种重要的面向对象编程的法则来削 ...

  7. 7. Docker Compose 项目

  8. hdu 5001(概率DP)

    Walk Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. C++的一大误区——深入解释直接初始化与复制初始化的区别

      转自:http://blog.csdn.net/ljianhui/article/details/9245661 不久前,在博客上发表了一篇文章——提高程序运行效率的10个简单方法,对于其中最后一 ...

  10. 洛谷 P1184高手之在一起 题解

    题目传送门 那位高手是谁啊?@jxpxcsh  QWQ. 这道题数据特别水,所以直接使用O(n*m),每读进一个m内的字符串,就扫一遍n的字符串.但注意地点字符串中有可能会有空格,所以这时候就要请出g ...