当前位置:网站首页>洛谷P4324 扭动的回文串
洛谷P4324 扭动的回文串
2022-08-11 04:00:00 【CLH_W】
题目描述
JYY有两个长度均为 NN 的字符串 AA 和 BB。
一个扭动字符串S(i,j,k)S(i,j,k) 由 AA 中的第 ii 个字符到第 jj 个字符组成的子串与 BB 中的第 jj 个字符到第 kk 个字符组成的子串拼接而成。
比如,若 A=A= ’XYZ’,B=B=’UVW’,则扭动字符串 S(1,2,3)=S(1,2,3)= ’XYVW’。
JYY 定义一个扭动的回文串为如下情况中的一个:
AA 中的一个回文串;
BB 中的一个回文串;
或者某一个回文的扭动字符串S(i,j,k)S(i,j,k)
现在 JYY 希望找出最长的扭动回文串。
输入格式
第一行包含一个正整数 NN。 第二行包含一个长度为 NN 的由大写字母组成的字符串 AA。 第三行包含一个长度为 NN 的由大写字母组成的字符串 BB。
输出格式
输出的第一行一个整数,表示最长的扭动回文串。
输入输出样例
输入 #1复制
5
ABCDE
BAECB
输出 #1复制
5
说明/提示
样例解释 最佳方案中的扭动回文串如下所示(不在回文串中的字符用 . 表示):
.BC…
…ECB
对于所有的数据,1 \leq n \leq 10 ^ 51≤n≤10
5
上代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
typedef unsigned long long ll;
using namespace std;
const int maxn=1e5+5,INF=0x3f3f3f3f,base=233;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=getchar();
return s*w;
}
int n,ans,len1[maxn],len2[maxn],len11[maxn],len22[maxn];
char a[maxn],b[maxn];
ll h1[maxn],g1[maxn],h2[maxn],g2[maxn],poww[maxn];
int Binary1(int l,int r,int now){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(h1[now]-h1[now-mid]*poww[mid]==g1[now]-g1[now+mid]*poww[mid])l=mid+1;
else r=mid-1;
}
return l-1;
}
int Binary11(int l,int r,int now){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(h1[now]-h1[now-mid]*poww[mid]==g1[now+1]-g1[now+1+mid]*poww[mid])l=mid+1;
else r=mid-1;
}
return l-1;
}
int Binary2(int l,int r,int now){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(h2[now]-h2[now-mid]*poww[mid]==g2[now]-g2[now+mid]*poww[mid])l=mid+1;
else r=mid-1;
}
return l-1;
}
int Binary22(int l,int r,int now){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(h2[now]-h2[now-mid]*poww[mid]==g2[now+1]-g2[now+1+mid]*poww[mid])l=mid+1;
else r=mid-1;
}
return l-1;
}
int Binary4(int l,int r,int nowl,int nowr){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(h1[nowl]-h1[nowl-mid]*poww[mid]==g2[nowr]-g2[nowr+mid]*poww[mid])l=mid+1;
else r=mid-1;
}
return l-1;
}
int main(){
freopen("A.in","r",stdin);
freopen("A.out","w",stdout);
n=read();
scanf(" %s %s",a+1,b+1);
poww[0]=1;
for(int i=1;i<=n;i++)h1[i]=h1[i-1]*base+a[i],h2[i]=h2[i-1]*base+b[i],poww[i]=poww[i-1]*base;
for(int i=n;i>=1;i--)g1[i]=g1[i+1]*base+a[i],g2[i]=g2[i+1]*base+b[i];
for(int i=1;i<=n;i++){
len1[i]=Binary1(1,min(i,n-i+1),i),len11[i]=Binary11(1,min(i,n-i+1),i);
ans=max(ans,max(len1[i]*2-1,len11[i]*2));
}
for(int i=1;i<=n;i++){
len2[i]=Binary2(1,min(i,n-i+1),i),len22[i]=Binary22(1,min(i,n-i+1),i);
ans=max(ans,max(len2[i]*2-1,len22[i]*2));
}
for(int i=1;i<=n;i++){
ans=max(ans,len1[i]*2-1+Binary4(0,n-len1[i]-i+2,i-len1[i],i+len1[i]-1)*2);
ans=max(ans,len11[i]*2+Binary4(0,n-len11[i]-i+1,i-len11[i],i+len11[i])*2);
ans=max(ans,len2[i]*2-1+Binary4(0,n-len2[i]-i+1,i-len2[i]+1,i+len2[i])*2);
ans=max(ans,len22[i]*2+Binary4(0,n-len22[i]-i+2,i-len22[i]+1,i+len22[i]+1)*2);
}
cout<<ans<<endl;
return 0;
}
边栏推荐
- leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》
- QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
- What problems should we pay attention to when building a programmatic trading system?
- Read the article, high-performance and predictable data center network
- Multi-serial port RS485 industrial gateway BL110
- The last update time of the tables queried by the two nodes of the rac standby database is inconsistent
- MYSQLg advanced ------ clustered and non-clustered indexes
- LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
- uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)
- [FPGA] day19- binary to decimal (BCD code)
猜你喜欢
【FPGA】day20-I2C读写EEPROM
2022-08-10 The sixth group Hiding spring study notes
Interchangeability and Measurement Techniques - Tolerance Principles and Selection Methods
获取Qt的安装信息:包括安装目录及各种宏地址
多串口RS485工业网关BL110
es-head插件插入查询以及条件查询(五)
《卫星界》刊评“星辰大海”计划:孙宇晨为太空旅游带来新的机遇
【组成原理 九 CPU】
【FPGA】SDRAM
[Likou] 22. Bracket generation
随机推荐
[C Language] Getting Started
A simple JVM tuning, learn to write it on your resume
Alibaba Cloud releases 3 high-performance computing solutions
STC8H development (15): GPIO drive Ci24R1 wireless module
什么是机器强化学习?原理是什么?
使用百度EasyDL实现智能垃圾箱
MYSQLg advanced ------ clustered and non-clustered indexes
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
shell监视gpu使用情况
En-us is an invalid culture error solution when Docker links sqlserver
【FPGA】SDRAM
获取Qt的安装信息:包括安装目录及各种宏地址
LeetCode814算题第15天二叉树系列值《814 二叉树剪枝》
A brief analysis of whether programmatic futures trading or manual order is better?
C语言 recv()函数、recvfrom()函数、recvmsg()函数
Common layout effect realization scheme
.NET Custom Middleware
(转)JVM中那些区域会发生OOM?
如何进行AI业务诊断,快速识别降本提效增长点?
Leetcode 108. 将有序数组转换为二叉搜索树