zoukankan      html  css  js  c++  java
  • HDU4513:完美队形II(Manacher)

    Description

     

    Input

     

    Output

     

    Sample Input

     

    Sample Output

     

    Solution

    才发现我之前不会证$Manacher$复杂度……QAQ

    题意是求最长向心非递减回文串。在$Manacher$函数向两边扩展的时候特判一下就好了。

    ┑( ̄Д  ̄)┍复杂度是对的啊……因为$Manacher$的时间复杂度证明和向两边扩的次数有关。

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #define N (200009)
     5 using namespace std;
     6 
     7 int T,n,x,tot,s[N],len[N];
     8 
     9 void Manacher()
    10 {
    11     int x,ans=0,mid=0,maxn=0;
    12     for (int i=1; i<=tot; ++i)
    13     {
    14         if (i>maxn) x=1;
    15         else x=min(maxn-i+1,len[mid*2-i]);
    16         while (s[i-x]==s[i+x] && s[i-x]<=s[i-x+2]) ++x;
    17         len[i]=x;
    18         if (i+x-1>maxn) maxn=i+x-1, mid=i;
    19         ans=max(ans,x-1);
    20     }
    21     printf("%d
    ",ans);
    22 }
    23 
    24 int main()
    25 {
    26     scanf("%d",&T);
    27     while (T--)
    28     {
    29         scanf("%d",&n);
    30         tot=0; s[++tot]=1e9; s[++tot]=-1;
    31         for (int i=1; i<=n; ++i)
    32             scanf("%d",&x), s[++tot]=x, s[++tot]=-1;
    33         s[++tot]=1e9;
    34         Manacher();
    35     }
    36 }
  • 相关阅读:
    python字符串相关的函数
    令人惊奇的gdb和pstack
    map的正确删除方式
    论道
    自动锁
    delete数组引发的core分析
    linux_硬链接和软链接区别
    vmware的卸载
    vmware + opensuse windows如何远程登录到suse上
    [置顶]援引个人新浪博客
  • 原文地址:https://www.cnblogs.com/refun/p/10040525.html
Copyright © 2011-2022 走看看