当前位置:网站首页>回文子串的最大长度(字符串哈希+二分)
回文子串的最大长度(字符串哈希+二分)
2022-06-29 17:47:00 【eva_can(not)survive】
139. 回文子串的最大长度 - AcWing题库高质量的算法题库https://www.acwing.com/problem/content/description/141/正序倒序求两次哈希,然后再通过枚举中点来求最长的回文子串(分偶和奇回文)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#define lowbit(x) ((x)&(-x))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using pll=pair<ll,ll>;
const int MAXN=1e6+5;
const int INF=0x3f3f3f3f;
const ll NNF=0x3f3f3f3f3f3f3f3f;
char s[MAXN];
ull h1[MAXN];
ull h2[MAXN];
ull k[MAXN];
const int p=131;
ull get1(int l,int r){
return h1[r]-h1[l-1]*k[r-l+1];
}
ull get2(int l,int r){
return h2[l]-h2[r+1]*k[r-l+1];
}
int main(){
int t=0;
while(scanf("%s",s+1)&&strcmp(s+1,"END")){
int n=strlen(s+1);
k[0]=1;
h2[n+1]=0;
for(int i=1;i<=n;i++){
h1[i]=h1[i-1]*p+(ull)s[i];
k[i]=k[i-1]*p;
}
for(int i=n;i>=1;i--){
h2[i]=h2[i+1]*p+(ull)s[i];
}
int ans=0;
for(int i=1;i<=n;i++){
int l=0,r=min(i-1,n-i);
while(l<=r){
int mid=l+r>>1;
if(get1(i-mid,i-1)==get2(i+1,i+mid)) l=mid+1;
else r=mid-1;
}
ans=max(ans,r<<1|1);
l=0,r=min(i,n-i);
while(l<=r){
int mid=l+r>>1;
if(get1(i-mid+1,i)==get2(i+1,i+mid)) l=mid+1;
else r=mid-1;
}
ans=max(ans,r<<1);
}
printf("Case %d: %d\n",++t,ans);
}
return 0;
}边栏推荐
- Does rapid software delivery really need to be at the cost of security?
- [Oracle] basic knowledge interview questions
- 使用autoIt 上传文件
- YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署
- Basic operations such as MySQL startup under Windows platform
- Sword finger offer 13 Robot range of motion (BFS)
- MATLAB 最远点采样(FPS)
- What is the SRM system? How do I apply the SRM system?
- SRM系统可以为企业带来什么价值?
- Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
猜你喜欢

分布式 | 几步快速拥有读写分离

第42期:MySQL 是否有必要多列分区

MATLAB 最远点采样(FPS)

Two controller layer interface authentication methods
![分割回文串[dp + dfs组合]](/img/7b/221b000984977508f849e19802c2c2.png)
分割回文串[dp + dfs组合]

VB. Net read / write NFC ntag tag source code

Inherit Chinese virtues, pay attention to the health of the middle-aged and the elderly, and Yurun milk powder has strong respect for the elderly

On adding and subtracting dates

关于日期相加减问题

位图的详细介绍及模拟实现
随机推荐
数字孪生能源系统,打造低碳时代“透视”眼
Visio标注、批注位置
Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
Visio annotation, annotation location
How MySQL queries character set codes of tables
Opencv+yolo-v3 for target tracking
Createstore for Redux source code analysis
Maidong Internet won the bid of Dajia Insurance Group
mysql. What is the concept of sock
[try to hack] cookies and sessions
The R language inputs the distance matrix to the hclust function for hierarchical clustering analysis. The method parameter specifies the distance calculation method between two combined data points,
PWM output experiment based on stm32f103zet6 library function
reflex
基于STM32F103ZET6库函数串口实验
Detailed introduction and Simulation of bitmap
力扣每日一题 06.29 两数相加
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a plot function visualization result to form the f
Bottom level internal skill cultivation
Segment tree and tree array template (copy and paste are really easy to use)
Basic operations such as MySQL startup under Windows platform