zoukankan      html  css  js  c++  java
  • Manacher(最长递减回文串)

    http://acm.hdu.edu.cn/showproblem.php?pid=4513

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

      1、挑出的人保持原队形的相对顺序不变,且必须都是在原队形中连续的;
      2、左右对称,假设有m个人形成新的队形,则第1个人和第m个人身高相同,第2个人和第m-1个人身高相同,依此类推,当然如果m是奇数,中间那个人可以任意;
      3、从左到中间那个人,身高需保证不下降,如果用H表示新队形的高度,则H[1] <= H[2] <= H[3] .... <= H[mid]。

      现在吉哥想知道:最多能选出多少人组成新的完美队形呢?
     
    Input
      输入数据第一行包含一个整数T,表示总共有T组测试数据(T <= 20);
      每组数据首先是一个整数n(1 <= n <= 100000),表示原先队形的人数,接下来一行输入n个整数,表示原队形从左到右站的人的身高(50 <= h <= 250,不排除特别矮小和高大的)。
     
    Output
      请输出能组成完美队形的最多人数,每组输出占一行。
     
    Sample Input
    2 3 51 52 51 4 51 52 52 51
     
    Sample Output
    3 4
     
    Source
     
    Recommend
    liuyiding
    题意:求最长递减回文串
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <iostream>
    #include <algorithm>
    #include <iostream>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include <stdio.h>
    #include <string.h>
    using namespace std;
    int a[100009] , b[200009];
    int p[200009];
    
    
    int num;
    
    
    int main()
    {
        int t ;
        scanf("%d" , &t);
        while(t--)
        {
            int l ;
            scanf("%d" , &l);
            for(int i = 0 ; i < l ; i++)
            {
                scanf("%d" , &a[i]);
            }
            int len = 0 ;
            b[len++] = -1;
            b[len++] = -1 ;
            for(int i = 0 ; i < l ; i++)
            {
                b[len++] = a[i];
                b[len++] = -1 ;
            }
            int mx = -1 , mid , ans = 0;
            for(int i = 1 ; i < len ; i++)
            {
                if(mx > i)
                {
                    p[i] = min(p[2*mid-i] , mx - i);
                }
                else
                {
                    p[i] = 1 ;
                }
                while(b[i-p[i]] == b[i+p[i]] &&  b[i - p[i] + 2] >= b[i - p[i]])//从中间往两边递减
                {
                    p[i]++;
                }
                if(mx < p[i] + i)
                {
                    mid = i ;
                    mx = p[i] + i;
                }
                ans = max(ans , p[i] - 1);
            }
            printf("%d
    " ,ans);
        }
    
        return 0 ;
    }
  • 相关阅读:
    位操作符:&位与、|位或、^异或、~求反、<<左移位、>>带符号右移位、>>>无符号右移位
    【leetcode】496. Next Greater Element I
    after_create and after_commit
    rails跳过回调的方法
    ruby执行周期性任务
    Nokogiri爬虫教程
    用ruby调用执行shell命令
    Redis使用详细教程
    rails中params[:id]与params["id"]分析
    mysql备份与恢复数据
  • 原文地址:https://www.cnblogs.com/nonames/p/11296260.html
Copyright © 2011-2022 走看看