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\n",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 }
posted @ 2018-11-29 20:06  Refun  阅读(162)  评论(0编辑  收藏  举报