hdu 5371 Hotaru's problem (Manacher算法)
来源:互联网 发布:王克晶java 编辑:程序博客网 时间:2024/05/04 17:55
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5371
题意:在输入的串里面找的一个连续的串,这个串由三部分组成且长度相等,第一部分s1与第二部分s2组成的串回文,第一部分s1与第三部分s3相同。
分析:由题可知,s1s2回文,s2s3回文。这里需要用Manacher算法处理出以每个位置为中心的最大回文半径。然后暴力枚举s1与s2交接处,判断是否存在合法的s1s2s3。
代码:
#include <iostream>#include <cstdio>#include <cstring> #include <algorithm>using namespace std;const int maxn = 1e6+7;int rad[maxn],str[maxn];void Manacher(int f[],int len){int id=0,mx=0,i,j;for(i=0;i<len;i++){f[i]=i>mx?1:min(f[2*id-i],mx-i);while(i-f[i]>=0 && i+f[i]<len && str[i+f[i]]==str[i-f[i]])++f[i];if(f[i]+i>mx){id=i;mx=f[i]+i;}}}int main(){int ncase,i,j,k,n,len;scanf("%d",&ncase);for(int T=1;T<=ncase;T++){scanf("%d",&n);memset(rad,0,sizeof(rad[0])*(2*n+2));len=0;str[len++]=-1;for(i=1;i<=n;i++){scanf("%d",&str[len++]);str[len++]=-1;}Manacher(rad,len);int ans=1;for(i=2;i<len;i+=2)for(j=rad[i];j>ans;j-=2)if(rad[i+j-1]>=j){ans=j;break;}printf("Case #%d: %d\n",T,(ans>>1)*3);}return 0;}
0 0
- HDU 5371 - Hotaru's problem(Manacher算法)
- hdu 5371 Hotaru's problem (Manacher算法)
- hdu 5371 Hotaru's problem (Manacher算法)
- HDU 5371 Hotaru's problem(Manacher算法+贪心)
- HDU 5371 Hotaru's problem(Manacher算法 回文串)
- HDU 5371 Hotaru's problem(Manacher算法)
- hdu 5371 Hotaru's problem (Manacher算法+枚举)
- Manacher算法(Hotaru's problem,HDU 5371)
- hdu 5371 Hotaru's problem【manacher】
- HDU 5371 Hotaru's problem Manacher+Set
- HDU 5371 Hotaru's problem(Manacher)
- [HDU 5371]Hotaru's problem:Manacher
- HDU 5371 Hotaru's problem (Manacher+暴力)
- HDU 5371 Hotaru's problem Manacher + set维护
- hdu 5371 Hotaru's problem(manacher+尺取法)
- hdu 5371 Hotaru's problem manacher + 线段树
- HDU 5371Hotaru's problem Manacher+线段树
- HDU 5371 Hotaru's problem(manacher + 枚举啊)
- 杭电 1301 Jungle Roads 【最小生成树&&Kruskal】
- 构造方法,文档编写
- Solr(5.2.1)安装、启动和建立索引
- Debian编译Android源码
- 提升vs2013、vs2015管理员权限win8.1
- hdu 5371 Hotaru's problem (Manacher算法)
- hdu 1181 变形课 DFS
- 如何限制访问一些网站
- fd to filename
- Lua数据库/MySQL操作
- 快速幂。素数筛选。辗转相除。
- HDU 1116 Play on Words
- the steps that may be taken to solve a feature selection problem:特征选择的步骤
- HDOJ 1162 Eddy's picture(最小生成树prim算法)