当前位置:网站首页>Maximum length of palindrome substring (string hash + binary)
Maximum length of palindrome substring (string hash + binary)
2022-06-29 17:56:00 【eva_ can(not)survive】
139. Maximum length of palindrome substring - AcWing Question bank High quality algorithm question bank https://www.acwing.com/problem/content/description/141/ Hash twice in positive and reverse order , Then we can find the longest palindrome substring by enumerating the midpoint ( Even and odd palindromes )
#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;
}边栏推荐
- How to create and delete MySQL triggers
- Have you grasped the most frequently asked question in the interview about massive data processing?
- 人脸识别4-百度商用方案调研
- 基于STM32F103ZET6库函数独立看门狗(IWDG)实验
- Teach you how to install the latest version of mysql8.0 database on windows, nanny level teaching
- Repair of JSON parsing errors in a collection
- Basic operations such as MySQL startup under Windows platform
- SRM供应商协同管理系统功能介绍
- phpunit骚操作之静态类的部分mock
- Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]
猜你喜欢

How to use the chart control of the b/s development tool devextreme - customize the axis position?

基于gis三维可视化的智慧城市行业运用

On adding and subtracting dates

NVIDIA installs the latest graphics card driver

kubekey2.2.1 kubernetes1.23.7离线包制作+harbor部暑并上传镜像

Have you grasped the most frequently asked question in the interview about massive data processing?

VB.Net读写NFC Ntag标签源码
![Split palindrome string [dp + DFS combination]](/img/7b/221b000984977508f849e19802c2c2.png)
Split palindrome string [dp + DFS combination]

2022春夏系列 KOREANO ESSENTIAL重塑时装生命力

What is the MySQL query view command
随机推荐
剖析下零拷贝机制的实现原理,适用场景和代码实现
Top 30 open source software
Opencv+YOLO-V3实现目标跟踪
Visual Studio插件CodeRush正式发布v22.1——优化调试可视化工具
lodash深拷贝使用
ABC253 D FizzBuzz Sum Hard(容斥定理)
Timer interrupt experiment based on stm32f103zet6 library function
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]
Two controller layer interface authentication methods
传承中华美德,关注中老年大健康,育润奶粉敬老情浓
Prevent form resubmission based on annotations and interceptors
How to solve the 2003 error of MySQL in Linux
DevCloud加持下的青软,让教育“智”上云端
Fill in the next right node pointer of each node [make good use of each point - > reduce the space-time complexity as much as possible]
Distributed | several steps of rapid read / write separation
Have you grasped the most frequently asked question in the interview about massive data processing?
How to create and delete MySQL triggers
Yurun multidimensional makes efforts in the charity field and bravely resists the corporate public welfare banner
MaxCompute字符串替换函数-replace
SSH protocol learning notes