hihocoder1032:(manacher)
来源:互联网 发布:酷家乐橱柜设计软件 编辑:程序博客网 时间:2024/05/14 06:17
题目:http://hihocoder.com/problemset/problem/1032
题目分析:manacher模板,RE了好多次。一开始是数组忘了开两倍长度,接下来又是多算了最后一位的答案导致数组越界(即’$’),重点是忘了写“if( i+temp[i]>p+temp[p] ) p=i;”这句如此重要的话……输出答案的时候要注意分类讨论一下。
CODE:
#include<iostream>#include<string>#include<cstring>#include<cmath>#include<cstdio>#include<cstdlib>#include<stdio.h>#include<algorithm>using namespace std;const int maxn=1000100;int temp[maxn<<1];string t,s;int n;int main(){freopen("c.in","r",stdin);freopen("c.out","w",stdout);scanf("%d",&n);for (int q=1; q<=n; q++){cin>>t;int tlen=t.size()-1;s="";s+='@';for (int i=0; i<tlen; i++){s+=t[i];s+='#';}s+=t[tlen];s+='$';int slen=s.size();int p=1,ans=0;temp[0]=temp[1]=0;for (int i=2; i<slen; i++){temp[i]=max( 0,min( temp[2*p-i],p+temp[p]-i ) );while ( s[i+temp[i]]==s[i-temp[i]-2] ) temp[i]++;if ( i+temp[i]>p+temp[p] ) p=i;if (s[i-1]=='#') ans=max(ans, temp[i]+(temp[i])%2 );else ans=max(ans, temp[i]+(temp[i]-1)%2 );}printf("%d\n",ans);}return 0;}
0 0
- hihocoder1032:(manacher)
- Manacher算法简介(HDU3068 hihocoder1032)
- hihocoder1032(最长回文子串manacher算法)
- manacher hihoCoder1032 最长回文子串
- hihocoder1032最长回文子串 [Manacher]
- 最长回文子串LPS(Hihocoder1032)
- hihocoder1032:最长回文字串
- hdu3068(manacher算法)
- Manacher(回文字符串)
- hdu 5340 (manacher)
- URAL1297(Manacher算法)
- poj3974(manacher)
- hdu4513(manacher)
- hdu3294(manacher)
- poj3974 Palindrome(manacher)
- hdu4513 (manacher)
- BZOJ3160(NTT+manacher)
- Luogu 3805(manacher)
- 最大堆的插入和删除
- std:weak_ptr
- 解析XML(PULL)
- Couldn't deserialize object in variable 'user1' 反序列化失败
- 如何一步一步用DDD设计一个电商网站(十四)—— 回顾与总结
- hihocoder1032:(manacher)
- collection 介绍
- qt 对话框关闭以及自动释放内存
- 数据结构堆排序
- java 冒泡排序
- ORA-00265:instance recovery required, cannot set ARCHIVELOG mode
- 文章标题
- set
- Android Studio 2.2新增布局——ConstraintLayout完全解析