hdu4513(manacher)

传送门:吉哥系列故事——完美队形II

题意:求最长回文队伍且队伍由中间向两边递减。

分析:manach算法小应用,在判断回文子串向两边递减时加点限制使回文是由中间向两边递减的。

#pragma comment(linker,"/STACK:1024000000,1024000000")
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <limits.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstdlib>
#include <stack>
#include <vector>
#include <set>
#include <map>
#define LL long long
#define mod 1000000007
#define inf 0x3f3f3f3f
#define eps 1e-6
#define N 100010
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PII pair<int,int>
using namespace std;
inline LL read()
{
    char ch=getchar();LL x=0,f=1;
    while(ch>‘9‘||ch<‘0‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch<=‘9‘&&ch>=‘0‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int p[N<<1],ans,len,num,mx,id;
int a[N],c[N<<1],n;
void build()
{
    n=read();
    for(int i=0;i<n;i++)a[i]=read();
    num=0;
    c[num++]=-1;c[num++]=inf;
    for(int i=0;i<n;i++)
    {
        c[num++]=a[i];
        c[num++]=inf;
    }
    c[num]=-2;
}
void manacher()
{
    ans=1;mx=0;
    memset(p,0,sizeof(p));
    for(int i=1;i<num;i++)
    {
        if(mx>i)p[i]=min(p[2*id-i],mx-i);
        else p[i]=1;
        while(c[i-p[i]]==c[i+p[i]]&&(c[i-p[i]]==inf||p[i]==1||c[i-p[i]]<=c[i-p[i]+2]))p[i]++;
        if(p[i]+i>mx)mx=p[i]+i,id=i;
        ans=max(ans,p[i]-1);
    }
}
int main()
{
    int T;
    T=read();
    while(T--)
    {
        build();
        manacher();
        printf("%d\n",ans);
    }
}

时间: 2024-08-01 19:39:43

hdu4513(manacher)的相关文章

吉哥系列故事——完美队形II(hdu4513+Manacher)

吉哥系列故事--完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1491    Accepted Submission(s): 555 Problem Description 吉哥又想出了一个新的完美队形游戏. 如果有n个人按顺序站在他的面前,他们的身高各自是h[1], h[2] ... h[n].吉哥希望从中挑出一些人,

hdu4513吉哥系列故事——完美队形II(manacher)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4513 manacher.稍微多了一点限制,延伸时要特殊处理一下. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn=200010; 6 int s[maxn<<1]; 7 int r[maxn<<1]

hdu----(4513)吉哥系列故事——完美队形II(manacher(最长回文串算法))

吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1012    Accepted Submission(s): 358 Problem Description 吉哥又想出了一个新的完美队形游戏! 假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让

HDU 4513 吉哥系列故事――完美队形II (manacher算法)

吉哥系列故事――完美队形II hdu-4513 Description 吉哥又想出了一个新的完美队形游戏!  假设有n个人按顺序站在他的面前,他们的身高分别是h[1], h[2] ... h[n],吉哥希望从中挑出一些人,让这些人形成一个新的队形,新的队形若满足以下三点要求,则就是新的完美队形: 1.挑出的人保持原队形的相对顺序不变,且必须都是在原队形中连续的:  2.左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然如果m是奇数,中

hdoj4513(Manacher简单应用)

题目链接:https://vjudge.net/problem/HDU-4513 题意:给定一个整型数组,求最大回文串,而且该回文串满足回文串中心的左边递增,右边递减. 思路: Manacher算法简单应用. 先用manacher算法求出p数组(p[i]表示以i为回文串中心的回文串最长是多少),然后从1到n遍历一遍,用len表示到第i个字符的不下降序列最长是多少,min(len , p[i])就是以i为回文串中心的满足题意的回文串的最长长度. AC code: #include<cstdio>

BZOJ 2084 [Poi2010]Antisymmetry(manacher)

[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2084 [题目大意] 对于一个01字符串,如果将这个字符串0和1取反后, 再将整个串反过来和原串一样,就称作“反对称”字符串. 比如00001111和010101就是反对称的,1001就不是. 现在给出一个长度为N的01字符串,求它有多少个子串是反对称的. [题解] 修改manacher的判定条件,对该串进行计算即可. [代码] #include <cstdio> #include

HDU 3068 最长回文 (manacher算法)

最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9188    Accepted Submission(s): 3159 Problem Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组

manacher马拉车算法

入门manacher最好文章:https://segmentfault.com/a/1190000003914228 我整理了模板代码:HDOJ3068马拉车模板 1 //讲解 https://segmentfault.com/a/1190000003914228 2 //manacher 算法模板 3 //求最长回文串 O(N) 4 #include <bits/stdc++.h> 5 using namespace std; 6 const int maxn=3e5+10; 7 char

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

题意:假设有n个人按顺序的身高分别是h[1], h[2] ... h[n],从中挑出一些人形成一个新的队形,新的队形若满足以下要求,则就是新的完美队形:  1.连续的 2.形成回文串 3.从左到中间那个人,身高需保证不下降 问有组成完美队形的最多人数 题解:Manacher算法的变形. 首先我们来解释一下Manacher算法:在我看来就是一个优化的暴力. 我们首先统一奇偶回文串成为奇数回文串,就是在两个数之间加入一些不可能出现的数. 例如题目:1 2 3 3 2 5 2 3 1—>(符号更加清楚