【manacher】hud 5371 Hotaru's problem

来源:互联网 发布:淘宝图库在哪里 编辑:程序博客网 时间:2024/05/23 02:21

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

//#pragma comment(linker, "/STACK:102400000,102400000")#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;typedef long long LL;typedef unsigned long long ULL;typedef pair<int,int> PII;const int INF = 0x3f3f3f3f;/*manacher 处理每个字符最长回文串, 暴力处理最长答案,贪心优化一下.*/const int N = 100010;int s[N*2], p[N*2];int main(){    int T,n,m;    scanf("%d",&T);    for(int cas=1; cas<=T; cas++){        scanf("%d",&n);        s[0] = -2;        for(int i=1; i<n*2; i+=2){            s[i] = -1;            scanf("%d",&s[i+1]);        }        s[2*n+1] = -1;        s[2*n+2] = -3;        m = 2*n+2;//        for(int i=1; i<=m; i++)//            printf("%d ",s[i]);//        puts("");        int pid=0;        p[0] = p[m] = 0;        for(int i=1; i<m; i++){            if(i<pid+p[pid]) p[i] = min(p[2*pid-i],p[pid]+pid-i);            else p[i] = 1;            while(s[i+p[i]]==s[i-p[i]]) p[i]++;            if(pid+p[pid] < i+p[i])                pid = i;        }        int ans = 0;        for(int i=1; i<m; i+=2){            for(int j=i+p[i]-1; j>ans+i; j-=2){                if(p[j] >= j-i) {                    ans = j-i;                    break;                }            }        }        printf("Case #%d: %d\n",cas,ans/2*3);    }    return 0;}