HDU-4513 吉哥系列故事——完美队形II Manacher算法

  题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4513

  当时比赛没有A掉的题目,在Manacher匹配的时候维护有序就可以了。

 1 //STATUS:C++_AC_156MS_2212KB
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<string.h>
 5 #include<math.h>
 6 #include<iostream>
 7 #include<string>
 8 #include<algorithm>
 9 #include<vector>
10 #include<queue>
11 #include<stack>
12 #include<map>
13 using namespace std;
14 #define LL __int64
15 #define pii pair<int,int>
16 #define Max(a,b) ((a)>(b)?(a):(b))
17 #define Min(a,b) ((a)<(b)?(a):(b))
18 #define mem(a,b) memset(a,b,sizeof(a))
19 #define lson l,mid,rt<<1
20 #define rson mid+1,r,rt<<1|1
21 const int N=100010,INF=0x3f3f3f3f,MOD=10000,STA=8000010;
22 const double DNF=1e13;
23 
24 int str[N<<1],s[N],p[N<<1];
25 int T,n,len;
26 
27 void Manacher(int *str,int *p)
28 {
29     int i,j,id,mx;
30     id=1,mx=1;
31     p[0]=p[1]=1;
32     for(i=2;i<n;i++){
33         p[i]=1;
34         if(mx>i){
35             p[i]=Min(p[(id<<1)-i],mx-i);
36         }
37         while(str[i+p[i]]==str[i-p[i]]){
38             if(!((i+p[i])&1) && str[i+p[i]-2]<str[i+p[i]])break;
39             p[i]++;
40         }
41         if(i+p[i]>mx){
42             id=i;
43             mx=i+p[i];
44         }
45     }
46 }
47 
48 void getstr(int *s)
49 {
50     int i;
51     str[0]='$';str[1]='#';
52     for(i=0;i<len;i++){
53         str[(i<<1)+2]=s[i];
54         str[(i<<1)+3]='#';
55     }
56     str[n]=0;
57 }
58 
59 int main()
60 {
61  //   freopen("in.txt","r",stdin);
62     int i,j,ans;
63     scanf("%d",&T);
64     while(T--)
65     {
66         scanf("%d",&len);
67         for(i=0;i<len;i++)
68             scanf("%d",&s[i]);
69         n=len*2+2;
70         getstr(s);
71         Manacher(str,p);
72 
73         ans=1;
74         for(i=2;i<n;i++){
75             ans=Max(ans,p[i]-1);
76         }
77 
78         printf("%d\n",ans);
79     }
80     return 0;
81 }

 

posted @ 2013-04-10 14:39  zhsl  阅读(551)  评论(0编辑  收藏  举报