题意:假设有n个人按顺序的身高分别是h[1], h[2] ... h[n],从中挑出一些人形成一个新的队形,新的队形若满足以下要求,则就是新的完美队形: 
  1、连续的
  2、形成回文串
  3、从左到中间那个人,身高需保证不下降

   问有组成完美队形的最多人数

题解:Manacher算法的变形。

首先我们来解释一下Manacher算法:在我看来就是一个优化的暴力。

我们首先统一奇偶回文串成为奇数回文串,就是在两个数之间加入一些不可能出现的数。

例如题目:1 2 3 3 2 5 2 3 1—>(符号更加清楚)$ # 1 # 2 # 3 # 3 # 2 # 5 # 2 # 3 # 1 # &(其中首尾多的#保证答案正确) ,注意首尾多增加 & 与 $ 为了不处理边界。接着我们只需要统计此时回文串长度的一半(包括中间一个字符)再减一就是此处总回文串的长度。

首先使用dp数组记录回文串一半长度,id记录最大dp的下标,maxid记录最大dp可以管到的最后一个距离,然后利用回文串的性质减少每次比较的次数

如图:在不超过maxid的位置(超过就不确定是否匹配成功了)j(i相对于id的对应位置)可以直接赋值给i,接着i再暴力寻找回文串是否没有找完整。最后我们的变形就在暴力处增加判断就好

#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
int num[Max];
int Init(int n)//增加其他字符,让奇数偶数回文串都变成奇数回文串
{
for(int i=n-;i>=;--i)
{
num[(i<<)+]=num[i];
num[(i<<)|]=;//不可能出现的字符
}
num[(n<<)|]=;
num[]=;//边界
num[(n<<)+]=;
return (n<<)+;
}
int dp[Max];//每个位置匹配长度
int Manacher(int n)
{
int manx=;
int id=,maxid=;
dp[]=;
for(int i=;i<n;++i)
{
if(maxid>=i)
dp[i]=min(dp[(id<<)-i],maxid-i+);
else
dp[i]=;
while(num[i+dp[i]]==num[i-dp[i]]&&(num[i+dp[i]]==||num[i+dp[i]]<=num[i+dp[i]-]))//注意这儿需要修改
dp[i]++;
if(maxid<i+dp[i]-)
{
maxid=i+dp[i]-;
id=i;
}
if(manx<dp[i]-)
manx=dp[i]-;
}
return manx;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<n;++i)
scanf("%d",&num[i]);
n=Init(n);
printf("%d\n",Manacher(n));
}
return ;
}

HDU 4513 吉哥系列故事——完美队形II (Manacher变形)的更多相关文章

  1. Hdu 4513 吉哥系列故事——完美队形II (manacher变形)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4513 题目描述: 打完题目描述了,点开题目,发现题目是中文,orz.jpg.果断又删掉了,习惯真可怕 ...

  2. HDU 4513 吉哥系列故事――完美队形II(Manacher)

    题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...

  3. HDU 4513 吉哥系列故事——完美队形II manacher

    吉哥系列故事——完美队形II Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希 ...

  4. HDU 4513 吉哥系列故事――完美队形II

    http://acm.hdu.edu.cn/showproblem.php?pid=4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) ...

  5. hdu 4513 吉哥系列故事——完美队形II (manachar算法)

    吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) P ...

  6. HDU 4513 吉哥系列故事——完美队形II(Manacher)

    Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成 ...

  7. HDU 4513 吉哥系列故事——完美队形II( Manacher变形 )

    链接:传送门 思路:根据完美队形的定义,可以得知,完美队形实质上是 回文串 + 序列出现峰,因为是在回文串中再次增加了一个要求,所以可以对 Manacher 进行改造,改造的部分应该为暴力匹配的循环 ...

  8. HDU 4513 吉哥系列故事——完美队形II

    变形的Manacher算法,在扩展的时候要加入限制条件,满足题目中说的从左到中间身高不减. 其他地方倒是没有什么改动.. //#define LOCAL #include <iostream&g ...

  9. HDU - 4513 吉哥系列故事――完美队形II(manacher)

    1.找出一个最长的回文子串,要求中间的值最大,然后向两侧递减. 2.判断条件改为:Ma[i+Mp[i]]==Ma[i-Mp[i]]&&Ma[i-Mp[i]]<=Ma[i-Mp[i ...

随机推荐

  1. ACM/ICPC 之 Prim范例(ZOJ1586-POJ1789(ZOJ2158))

    两道Prim解法范例题型,简单的裸Prim,且两题相较以边为重心的Kruskal解法而言更适合以点为重心扩展的Prim解法. ZOJ1586-QS Network 题意:见Code 题解:直接的MST ...

  2. gtk+-3.21.4 static build step in windows XP

    In recent days the weather is very hot Unable to sleep properly Under the state of daze research gtk ...

  3. ffmpeg-20160515-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  4. filter 简介

    概述 filter() 方法使用指定的函数测试所有元素,并创建一个包含所有通过测试的元素的新数组. 语法 var new_arrary = arr.filter(callback[, thisArg] ...

  5. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  6. SSH框架中json传递失败

    错误截图: 这个错误乍一看无从下手,报的都是框架底层的错误,于是查阅资料得到了答案. 错误原因:struts会将action中定义的一些变量序列化转换成json格式,需要调用对象的一系列get方法,并 ...

  7. asp.net Excel数据导入到数据库中

    protected void Btn_Import_Click(object sender, EventArgs e) { bool Result_Import = false; bool Resul ...

  8. 在VC中创建并调用DLL

    转自:http://express.ruanko.com/ruanko-express_45/technologyexchange6.html 一.DLL简介 1.什么是DLL? 动态链接库英文为DL ...

  9. Swift - 多行文本输入框(UITextView)

    1,多行文本控件的创建 1 2 3 4 let textview = UITextView(frame:CGRect(x:10, y:100, width:200, height:100)) text ...

  10. Swift - enumerateObjectsUsingBlock的用法

    enumerateobjectsusingblock:不是Array的方法在NSArray使用.如果你想使用它,你需要一个实例NSArray而不是Array. import Foundation va ...