51 Nod 1089 最长回文子串(Manacher算法)
2024-09-02 23:15:01
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。
输入一个字符串Str,输出Str里最长回文子串的长度。
Input
输入Str(Str的长度 <= 100000)
Output
输出最长回文子串的长度L。
Input示例
daabaac
Output示例
5
#include<bits/stdc++.h>
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<math.h>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<algorithm>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn=110010;
char Ma[maxn*2];
int Mp[maxn*2];
void Manacher(char s[],int len)
{
int l=0;
Ma[l++]='$';
Ma[l++]='#';
for(int i=0;i<len;i++)
{
Ma[l++]=s[i];
Ma[l++]='#';
}
Ma[l]=0;
int mx=0,id=0;
for(int i=0;i<l;i++)
{
Mp[i]=mx>i?min(Mp[2*id-i],mx-i):1;
while(Ma[i+Mp[i]]==Ma[i-Mp[i]])Mp[i]++;
if(i+Mp[i]>mx)
{
mx=i+Mp[i];
id=i;
}
}
}
char s[maxn];
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLIN
while(scanf("%s",s)==1)
{
int len=strlen(s);
Manacher(s,len);
int ans=0;
for(int i=0;i<2*len+2;i++)
ans=max(ans,Mp[i]-1);
printf("%d\n",ans);
}
return 0;
}
最新文章
- (转)配置Log4j(很详细)
- easyUI学习笔记之tab组件的鼠标事件
- 【python】list,dict赋值不要用等号,要用extend,update
- bzoj 1588: [HNOI2002]营业额统计 treap
- IntelliJ IDEA 中properties中文显示问题
- python 生成二维码
- 【Node.app】Node.js for iOS
- 配置phpmyadmin使登录时可填写IP管理多台MySQL 连接多个数据库 自动登录
- iOS中打印系统详细日志
- Qt对话框_模态/非模态
- Mysql主从复制读写分离
- index-document-shard
- Pytorch中的Batch Normalization操作
- PHP中两个冒号是什么意思
- poj1679
- Vue03
- 34.Find First and Last Position of Element in Sorted Array---头条面试题、《剑指offer》38
- nmap速查表v1.0
- 怎样在QML应用中创建一个Context Menu
- [android] 两种异步方式