HDU5371——Manacher+set维护——Hotaru's problem

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

/*
先用Manacher算法得出最长回文子串,然后用set维护ans的值
对所有回文的长度进行排序, 那么之后的点如果覆盖了最接近的点那么那么点肯定是覆盖了当前点,用二分得到最近不大于u的距离
S.upper_bound(id+u) 从起始到末尾得到第一个大于id+u的迭代器,
S.lower_bound(id-u) 从起始到末尾得到第一个不小于id-u的迭代器
set 容器自动从小到大排序
*/
/************************************************
* Author        :Powatr
* Created Time  :2015-8-12 19:49:44
* File Name     :1003.cpp
 ************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAXN = 4e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

int n;
int p[MAXN];
int len; 
int a[MAXN];
int aa[MAXN];
set<int> S;
set<int>::iterator it;
struct edge{
    int u, id;
}b[MAXN];
bool cmp(edge i, edge j)
{
    return i.u > j.u;
}
void inti()
{
    memset(p, 0, sizeof(p));
    memset(aa, -1, sizeof(aa));
    for(int i = 1; i <= n; i++)
        aa[i*2] = a[i];
    len = n*2 + 1;
}

int read()
{
    char z = getchar();
    while(z < '0' || z > '9') z = getchar();
    int ans = 0;
    while( z >= '0' && z <= '9'){
        ans = ans*10 + z - '0';
        z = getchar();
    }
    return ans;
}
void Manacher()
{
    int mx = 0;
    int id;
    for(int i = 1; i <= len; i++){
        if(mx > i){
            p[i] = min(p[2*id-i], mx - i);
        }
        else p[i] = 1;
        for( ;aa[i+p[i]] == aa[i-p[i]]; p[i]++);
        if(p[i] + i > mx){
            mx = p[i] + i;
            id = i;
        }
    }
}

int main(){
   int T;
   scanf("%d", &T);
   for(int cas = 1; cas <= T; cas++){
       S.clear();
       n = read();
       for(int i = 1; i <= n; i++)
           a[i] = read();
       inti();
       Manacher();
       for(int i = 1; i <= n; i++){
           b[i].u = (p[i*2+1]-1)/2;
           b[i].id = i;
       }
       sort(b+1, b+n+1, cmp);
       int ans = 0;
       for(int i = 1; i <= n; i++){
           int id = b[i].id;
           int u = b[i].u;
           if(u == 0) break;
           S.insert(id);
           it = S.upper_bound(id+u);
           it--;
           ans = max(ans, *it - id);
           it = S.lower_bound(id-u);
           ans = max(ans, id - *it);
       }
       printf("Case #%d: ", cas);
       printf("%d\n", 3*ans);
   }
   return 0;
}

  

转载于:https://www.cnblogs.com/zero-begin/p/4725504.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值