Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法)
题意:
取一字符串不相交的前缀和后缀(可为空)构成最长回文串。
思路:
先从两边取对称的前后缀,之后再取余下字符串较长的回文前缀或后缀。
#include <bits/stdc++.h>
using namespace std;
string Manacher(const string &s){
string t="#";
for(char c:s) t+=c,t+="#";
int RL[t.size()]={0};
int MaxRight=0;
int pos=0;
for(int i=0;i<t.size();i++){
if(i<MaxRight)
RL[i]=min(RL[2*pos-i],MaxRight-i);
else
RL[i]=1;
while(i-RL[i]>=0&&i+RL[i]<t.size()&&t[i-RL[i]]==t[i+RL[i]])
RL[i]+=1;
if(RL[i]+i-1>MaxRight)
pos=i,MaxRight=RL[i]+i-1;
}
int MaxLen=0;
for(int i=0;i<t.size();i++)
if(RL[i]==i+1)
MaxLen=i;
return s.substr(0,MaxLen);
}
void solve(){
string s;cin>>s;
int l=0,r=s.size()-1;
while(l<r&&s[l]==s[r]) ++l,--r;
string s1=s.substr(l,r-l+1);
string s2=s1;
reverse(s2.begin(),s2.end());
s1=Manacher(s1);
s2=Manacher(s2);
cout<<s.substr(0,l)<<(s1.size()>s2.size()?s1:s2)<<s.substr(r+1)<<"\n";
}
int main()
{
int t;cin>>t;
while(t--)
solve();
return 0;
}
Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法)的更多相关文章
- Codeforces Global Round 7 D2. Prefix-Suffix Palindrome (Hard version)(Manacher算法+输出回文字符串)
This is the hard version of the problem. The difference is the constraint on the sum of lengths of s ...
- CodeForces Global Round 1
CodeForces Global Round 1 CF新的比赛呢(虽然没啥区别)!这种报名的人多的比赛涨分是真的快.... 所以就写下题解吧. A. Parity 太简单了,随便模拟一下就完了. B ...
- Codeforces Global Round 1 - D. Jongmah(动态规划)
Problem Codeforces Global Round 1 - D. Jongmah Time Limit: 3000 mSec Problem Description Input Out ...
- Codeforces Global Round 2 题解
Codeforces Global Round 2 题目链接:https://codeforces.com/contest/1119 A. Ilya and a Colorful Walk 题意: 给 ...
- Codeforces Global Round 1 (A-E题解)
Codeforces Global Round 1 题目链接:https://codeforces.com/contest/1110 A. Parity 题意: 给出{ak},b,k,判断a1*b^( ...
- Codeforces Global Round 3
Codeforces Global Round 3 A. Another One Bites The Dust 有若干个a,有若干个b,有若干个ab.你现在要把这些串拼成一个串,使得任意两个相邻的位置 ...
- Codeforces Global Round 1 (CF1110) (未完结,只有 A-F)
Codeforces Global Round 1 (CF1110) 继续补题.因为看见同学打了这场,而且涨分还不错,所以觉得这套题目可能会比较有意思. 因为下午要开学了,所以恐怕暂时不能把这套题目补 ...
- 【手抖康复训练1 】Codeforces Global Round 6
[手抖康复训练1 ]Codeforces Global Round 6 总结:不想复习随意打的一场,比赛开始就是熟悉的N分钟进不去时间,2333,太久没写题的后果就是:A 题手抖过不了样例 B题秒出思 ...
- Codeforces Global Round 11 个人题解(B题)
Codeforces Global Round 11 1427A. Avoiding Zero 题目链接:click here 待补 1427B. Chess Cheater 题目链接:click h ...
随机推荐
- SpringBoot整合Shiro完成验证码校验
SpringBoot整合Shiro完成验证码校验 上一篇:SpringBoot整合Shiro使用Redis作为缓存 首先编写生成验证码的工具类 package club.qy.datao.utils; ...
- docker 报错: Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
最近在 Windows 子系统 WSL 上面安装了一个 ubuntu18.04, 安装完docker 跑 hello-world 的时候报错了 docker: Cannot connect to th ...
- postgresql插件安装
postgresql安装包自带插件安装: 1.编译安装插件 # root用户 # postgresql安装过程省略 # 进入解压包的contrib目录 cd postgresql-10.6/contr ...
- [Noip模拟题]Seq
题目描述 由于hyf长得实在是太帅了,英俊潇洒,风流倜傥,人见人爱,花见花开,车见车载.有一群MM排队看hyf.每个MM都有自己独特的风格,由于hyf有着一颗包容的心,所以,什么风格的MM他都喜欢-- ...
- [Usaco2008 Mar]River Crossing渡河问题
题目描述 Farmer John以及他的N(1 <= N <= 2,500)头奶牛打算过一条河,但他们所有的渡河工具,仅仅是一个木筏. 由于奶牛不会划船,在整个渡河过程中,FJ必须始终在木 ...
- kettle数据质量统计
1.利用Kettle的"分组","JavaScript代码","字段选择"组件,实现数据质量统计.2.熟练掌握"JavaScrip ...
- OpenCV 和 Dlib 人脸识别基础
00 环境配置 Anaconda 安装 1 下载 https://repo.anaconda.com/archive/ 考虑到兼容性问题,推荐下载Anaconda3-5.2.0版本. 2 安装 3 测 ...
- MATLAB图像处理_Bayer图像处理 & RGB Bayer Color分析
Bayer图像处理 Bayer是相机内部的原始图片, 一般后缀名为.raw. 很多软件都可以查看, 比如PS. 我们相机拍照下来存储在存储卡上的.jpeg或其它格式的图片, 都是从.raw格式转化 ...
- 大促密集,CDN如何保障电商体验如丝般顺滑?
简介: 前不久,阿里云技术天团空降CSDN在线峰会,对核心技术竞争力进行解读.其中,阿里云高级技术专家曾福华分享了<双11: CDN如何保障电商大促如丝般顺滑>的议题.俗话说:养兵千日,用 ...
- 导出带有图片的excel
public static void main(String[] args) { try { FileOutputStream out = new FileOutputStream("d:\ ...