51nod 1089 最长回文子串 V2(Manacher算法)
来源:互联网 发布:java中poi是什么意思 编辑:程序博客网 时间:2024/06/01 10:24
1089 最长回文子串 V2(Manacher算法)
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
回文串是指aba、abba、cccbccc、aaaa这种左右对称的字符串。
输入一个字符串Str,输出Str里最长回文子串的长度。
Input
输入Str(Str的长度 <= 100000)
Output
输出最长回文子串的长度L。
Input示例
daabaac
Output示例
5
马拉车算法
#include<iostream>using namespace std;#include<string>#define min(x, y) ((x)<(y)?(x):(y))#define max(x, y) ((x)<(y)?(y):(x))string findLongestPalindrome3(string s){ int length=s.size(); for(int i=0,k=1;i<length-1;i++)//给字符串添加 # { s.insert(k,"#"); k=k+2; } length=length*2-1;//添加#后字符串长度 int *rad=new int[length](); rad[0]=0; for(int i=1,j=1,k;i<length;i=i+k) { while(i-j>=0&&i+j<length&&s.at(i-j)==s.at(i+j)) j++; rad[i]=j-1; for(k=1;k<=rad[i]&&rad[i-k]!=rad[i]-k;k++)//镜像,遇到rad[i-k]=rad[i]-k停止,这时不用从j=1开始比较 rad[i+k]=min(rad[i-k],rad[i]-k); j=max(j-k,0);//更新j } int max=0; int center; for(int i=0;i<length;i++) { if(rad[i]>max) { max=rad[i]; center=i; } } return s.substr(center-max,2*max+1);}int main(){ string str; cin>>str; str=findLongestPalindrome3(str); int cnt=0; for(int i=0;i<str.length();i++) { if(str[i]!='#') cnt++; } cout<<cnt<<endl; return 0;}
阅读全文
0 0
- 51nod 最长回文子串 V2(Manacher算法)
- 51nod 1089 最长回文子串 V2 (Manacher算法)
- 51nod-【1089 最长回文子串 V2(Manacher算法)】
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 【51nod】1089 最长回文子串 V2(Manacher算法)
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 51Nod 1089 最长回文子串 V2(Manacher算法)
- 51nod--1089 最长回文子串V2(Manacher 算法)
- 51Nod 1089 最长回文子串 V2(Manacher算法)
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 51Nod-1089 最长回文子串 V2(Manacher算法)
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 51Nod 1089 最长回文子串 V2(Manacher算法)
- 51Nod 1089 最长回文子串 V2 —— Manacher算法
- 51nod1089---最长回文子串 V2(51nod基础:Manacher算法)
- 51nod1089 最长回文子串 V2(Manacher算法)
- 51nod_1089 最长回文子串 V2(Manacher算法)
- 51nod 1089 最长回文字串V2(Manacher算法)
- c语言题(1)
- 登录注册+接口
- zuul No route found for uri:
- 认识Linux数据重定向redirection
- 安卓蓝牙bluetooth开发全解
- 51nod 1089 最长回文子串 V2(Manacher算法)
- 动态代理模式和责任链模式
- 53. Maximum Subarray
- LeetCode刷题(14)
- 心得,在工作中学会”忽悠”别人
- 网狐棋牌 消息命令
- 请求网络数据实现登陆注册
- justify-content的属性详解
- IE6兼容适配问题