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 ...
随机推荐
- Cisco常用命令
• 首次配置网络设备 ○ 需要使用Console线连接进行初始化配置 § 在PC使用"超级终端"或其他软件. • 交换机的工作模式: ...
- torch.nn 的本质
torch.nn 的本质 PyTorch 提供了各种优雅设计的 modules 和类 torch.nn,torch.optim,Dataset 和 DataLoader 来帮助你创建并训练神经网络.为 ...
- MongoDB导出导入功能
导出脚本: mongo_export.sh !#/bin/bash mongoexport -h x.x.x.x --port 27017 -d database -c collection -q ...
- (十)Python装饰器
装饰器:本质就是函数,功能是为其他函数添加附加功能. 两个原则: 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 一个栗子 def test(): res = 0 for i in ra ...
- 使用msys2在window下构建和使用Linux的软件
目录 前言 安装 使用 总结 前言 在window下构建Linux编译环境是很常见的,以前用过mingw弄过差不多的环境. 但是使用msys2后就根本停不下来咯,太好用咯. 安装 去官网下载吧,安装跟 ...
- 【Oracle】Script to Collect DRM Information (drmdiag.sql) (文档 ID 1492990.1)
脚本对应如下: The following (drmdiag.sql) is a script to collect information related to DRM (Dyanamic Reso ...
- SQL语句中 ` 的作用
SQL语句中 ` 的作用 做攻防世界WEB区 supersqli 题目,在构建SQL语句时,遇到SQL语句中有 ` 时可以解析,没有则不能. 查阅资料得知,` 通常用来说明其中的内容是数据库名.表名. ...
- Pandas的数据分组-aggregate聚合
在对数据进行分组之后,可以对分组后的数据进行聚合处理统计. agg函数,agg的形参是一个函数会对分组后每列都应用这个函数. import pandas as pd import numpy as n ...
- js12种应该注意的地方
1. == Javascript有两组相等运算符,一组是==和!=,另一组是===和!==.前者只比较值的相等,后者除了值以外,还比较类型是否相同. 请尽量不要使用前一组,永远只使用===和!==.因 ...
- 新编日语1234册/重排本/全册 pdf
网上找的资源链接大部分都失效了,无奈之下只好淘宝购买.顺便分享一下吧. 链接: https://pan.baidu.com/s/1v5-osHKrIPzlgpd8yNIP5Q 提取码: kexn