当前位置:网站首页>洛谷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 Brush Questions Day 11 String Series "58 Last Word Length"
- 使用百度EasyDL实现森林火灾预警识别
- The custom of the C language types -- -- -- -- -- - structure
- 直播平台开发,Flutter,Drawer侧滑
- 【C语言】入门
- App Basic Framework Construction丨Log Management - KLog
- EasyCVR接入GB28181设备时,设备接入正常但视频无法播放是什么原因?
- 多串口RS485工业网关BL110
- Leetcode 450. 删除二叉搜索树中的节点
- A brief analysis of whether programmatic futures trading or manual order is better?
猜你喜欢
![[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface](/img/cb/41e5f553b0b776dccf0e39f9bf377f.png)
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface

STC8H开发(十五): GPIO驱动Ci24R1无线模块
![Binary tree related code questions [more complete] C language](/img/85/a109eed69cd54be3c8290e8dd67b7c.png)
Binary tree related code questions [more complete] C language

一文读懂 高性能可预期数据中心网络

北湖区燕泉街道开展“戴头盔·保安全”送头盔活动

【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion

"104 Maximum Depth of Binary Trees" in LeetCode's Day 12 Binary Tree Series

leetCode刷题14天二叉树系列之《 110 平衡二叉树判断》

Provincial level of Echart maps, as well as all prefecture-level download and use

When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
随机推荐
LeetCode刷题第12天二叉树系列之《104 二叉树的最大深度》
我的 archinstall 使用手册
Leetcode 450. 删除二叉搜索树中的节点
Binary tree related code questions [more complete] C language
Uni - app - access to Chinese characters, pinyin initials (according to the Chinese get pinyin initials)
LeetCode刷题第16天之《239滑动窗口最大值》
Where can machine learning be applied?What is machine learning useful for?
console.log alternatives you didn't know about
2022-08-10 The sixth group Hiding spring study notes
How can users overcome emotional issues in programmatic trading?
机器学习怎么学?机器学习流程
【FPGA】day19-二进制转换为十进制(BCD码)
机器学习可以应用在哪些场景?机器学习有什么用?
js 将字符串作为js执行代码使用
使用百度EasyDL实现施工人员安全装备检测
直播平台开发,Flutter,Drawer侧滑
【FPGA】名词缩写
Common layout effect realization scheme
【力扣】22.括号生成
uni-app - 获取汉字拼音首字母(根据中文获取拼音首字母)