博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3068 最长回文( Manacher模板题 )
阅读量:6874 次
发布时间:2019-06-26

本文共 1230 字,大约阅读时间需要 4 分钟。


链接:

思路:Manacher模板题,寻找串中的最长回文子串


/*************************************************************************    > File Name: hdu3068.cpp    > Author:    WArobot     > Blog:      http://www.cnblogs.com/WArobot/     > Created Time: 2017年05月19日 星期五 13时09分43秒 ************************************************************************/#include
using namespace std;const int MAX_N = 110000*3;char s[MAX_N] , str[MAX_N];int len1 , len2 , p[MAX_N] , ans;// 对字符串进行预处理void init(){ len1 = strlen(s); str[0] = '$'; str[1] = '#'; for(int i = 0 ; i < len1 ; i++){ str[i*2+2] = s[i]; str[i*2+3] = '#'; } len2 = len1*2 + 2; str[len2] = '*';}void Manacher(){ int id , mx = 0; for(int i = 1 ; i < len2 ; i++){ if( mx > i ) p[i] = min( p[2*id-i] , mx-i); else p[i] = 1; for(; str[i+p[i]] == str[i-p[i]] ; p[i]++); // 暴力匹配一下能更新的最大长度 if( p[i] + i > mx ) mx = p[i] + i , id = i; }}int main(){ while(~scanf("%s",s)){ init(); Manacher(); ans = 0; for(int i = 1 ; i < len2 ; i++){ ans = max( ans , p[i] ); } printf("%d\n",ans-1); } return 0;}

转载于:https://www.cnblogs.com/WArobot/p/6882327.html

你可能感兴趣的文章
LNMMP架构的安装配置和功能的实现
查看>>
几个设置让你的邮箱不会爆满
查看>>
我的友情链接
查看>>
在linux6上安装RAC时多路径的权限设置
查看>>
[转载] 七龙珠第一部——第037话 忍者出现
查看>>
网络数据通信加密系统中加密解密流程
查看>>
PXE+KickStart无人值守安装RHEL
查看>>
十年,站酷已成设计论坛霸主,博客园却成无兵之将
查看>>
ansible安装
查看>>
使用bind搭建DNS服务器
查看>>
Windows server 2008R2 DHCP服务器
查看>>
计算机网络笔记--数据链路层(一)
查看>>
我的友情链接
查看>>
Java方法重载注意事项
查看>>
爱创课堂每日一题第五十九天- javascript继承的6种方法
查看>>
16.1 Tomcat介绍 16.2 安装jdk 16.3 安装Tomcat
查看>>
JS 正则表达式用法
查看>>
文档查看cat_more_less_head_tail
查看>>
python课堂笔记之django-day01(4)
查看>>
九月十九日作业
查看>>