题意:在一个字符串里面找最长的[A][B][A]子串,其中[A][B]是回文串,[A]和[B]的长度相等

思路:[A][B]是回文串,所以[B][A]也是回文串。先预处理出每个点的最大回文半径Ri,枚举[A][B]的对称轴位置p,那么就是要找最大的一个[B][A]的对称轴位置i,满足i<=p+R[p],i-R[i]<=p。由于p是递增的,先前满足的以后肯定满足,于是可以用set来维护i-R[i]<=p的所有的位置i的集合,并可在logN的时间内得到最大的位置i。

  1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define X first
#define Y second
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a)) typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull; //#ifndef ONLINE_JUDGE
void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
//#endif
template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
template<typename T>
void V2A(T a[],const vector<T>&b){for(int i=;i<b.size();i++)a[i]=b[i];}
template<typename T>
void A2V(vector<T>&a,const T b[]){for(int i=;i<a.size();i++)a[i]=b[i];} const double PI = acos(-1.0);
const int INF = 1e9 + ;
const double EPS = 1e-8; /* -------------------------------------------------------------------------------- */ const int maxn = 2e5 + ; struct StringHash {
const static unsigned int hack = ;
//const static int maxn = 2e5 + 7;
unsigned long long H[maxn], C[maxn];
void init(int s[], int n) {
for (int i = ; i < n; i ++) {
H[i] = (i? H[i - ] * hack : ) + s[i];
}
C[] = ;
for (int i = ; i <= n; i ++) C[i] = C[i - ] * hack;
}
unsigned long long get(int L, int R) {
return H[R] - (L? H[L - ] * C[R - L + ] : );
}
} ;
StringHash hsh, hshrev;
vector<int> G[maxn];
int a[maxn], b[maxn], F[maxn];
set<int> S;
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
int T, n, cas = ;
cin >> T;
while (T --) {
cin >> n;
for (int i = ; i < n; i ++) {
scanf("%d", a + i);
}
hsh.init(a, n);
for (int i = ; i < n; i ++) b[i] = a[n - i - ];
hshrev.init(b, n);
int total = * n - ;
for (int i = ; i < total; i ++) {
int L = i / , R = (i + ) / ;
int minlen = , maxlen = min(L + , n - R);
while (minlen < maxlen) {
int midlen = (minlen + maxlen + ) >> ;
int lpos = L - midlen + , rpos = R + midlen - ;
if (hsh.get(lpos, L) == hshrev.get(n - rpos - , n - R - ))
minlen = midlen;
else maxlen = midlen - ;
}
F[i] = minlen;
}
S.clear();
for (int i = ; i < n; i ++) G[i].clear();
int ans = ;
for (int i = ; i < n - ; i ++) {
G[i - F[ * i + ] + ].pb(i);
}
for (int i = ; i < G[].size(); i ++) S.insert(G[][i]);
for (int i = ; i < total; i += ) {
int L = i / ;
for (int j = ; j < G[L + ].size(); j ++) S.insert(G[L + ][j]);
if (S.size()) {
set<int>::iterator R = S.upper_bound(L + F[i]); R --;
umax(ans, *R - L);
}
}
printf("Case #%d: %d\n", ++ cas, ans * );
} return ;
}

[hdu5371 Hotaru's problem]最大回文半径的更多相关文章

  1. [2015hdu多校联赛补题]hdu5371 Hotaru's problem

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 题意:把一个数字串A翻过来(abc翻过来为cba)的操作为-A,我们称A-AA这样的串为N-se ...

  2. Manacher算法求回文半径

    http://wenku.baidu.com/link?url=WFI8QEEfzxng9jGCmWHoKn0JBuHNfhZ-tKTDMux34CeY8UNUwLVPeY5HA3TyoKU2XegX ...

  3. HDU5371 Hotaru's problem

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  4. hdu5371 Hotaru's problem

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  5. Gym - 101981M:(南京) Mediocre String Problem(回文树+exkmp)

    #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) using ...

  6. HDU 5371——Hotaru's problem——————【manacher处理回文】

    Hotaru's problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. Manacher HDOJ 5371 Hotaru's problem

    题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...

  8. BZOJ 2342 回文串-Manacher

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2342 思路:先跑一遍Manacher求出p[i]为每个位置为中心的回文半径,因为双倍回文串 ...

  9. BZOJ 2342 双倍回文(manacher算法)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2342 题意:定义双倍回文串为:串的长度为4的倍数且串的前一半.后一半.串本身均是回文的. ...

随机推荐

  1. Linux命令:chown

    说明: 将指定文件的拥有者改为指定的用户或组. 语法: chown [-cfhvR] [--help] [--version] user[:group] file... 参数: user : 新的文件 ...

  2. v&n赛 ML 第一步(python解决)

    题目链接 给了70组x,y,根据提示,是求拟合曲线,再通过x求y 知道MATLAB应该录入就能解决吧,但是没下这软件,试试用python解决 #coding:utf- from pwn import ...

  3. 杂记三 &#183; CSP-2019-The first step

    update:我终于懂得衰亡的民族之所以沉默的缘由了. 初赛Day -7 虽然我是第一次参加初赛而且到现在为止我还没见过初赛题但我一点也不慌! 真的!一点!也不慌! 初赛Day 1 早上和可s爱b j ...

  4. tensorflow1.0 变量加法

    import tensorflow as tf state = tf.Variable(0,name='counter') print(state.name) one = tf.constant(1) ...

  5. ansible的清单管理与模块应用(三)

  6. ansible的模块使用

    转载于   https://www.cnblogs.com/franknihao/p/8631302.html [Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和 ...

  7. Ubuntu 18.04更换apt-get源

    使用apt-get安装时,会很慢,更换了国内的源后,就可以解决这个问题了. 1. 备份sources.list文件 sudo cp /etc/apt/sources.list /etc/apt/sou ...

  8. Spring Boot中的Properties

    文章目录 简介 使用注解注册一个Properties文件 使用属性文件 Spring Boot中的属性文件 @ConfigurationProperties yaml文件 Properties环境变量 ...

  9. js 函数对象的继承 inherit 带 插件完整解析版[helpers.js]

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS的对象继承方法 需求分析: 1. ...

  10. android 动画学习总结

    本文内容是本人阅读诸多前辈的学习心得后整理的,若有雷同,请见谅 Android 动画 分类:帧动画,补间动画,属性动画  . 1.帧动画 将一张张单独的图片连贯的进行播放,从而在视觉上产生一种动画的效 ...