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

[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. 美化你的终端利器Iterm2

    Iterm2是特别好用的一款终端,支持自定义字体和高亮,让日常开发,充满愉悦. 安装iterm2(mac版) brew tap caskroom/cask brew cask install iter ...

  2. tensorflow1.0 变量加法

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

  3. JVM致命错误日志详解

    目录 文件描述 文件位置 文件头 错误信息记录 JVM运行信息 崩溃原因 错误信息 线程描述 线程信息 信号信息 计数器信息 机器指令 内存映射信息 线程堆栈 其他信息 进程描述 线程列表 虚拟机状态 ...

  4. 免费申请通配符类型SSL证书

    折腾起因 最近做了个小网站wawoo.fun,一个做mac壁纸的小网站,网站还处在初级阶段,不能跟大神的比.网站发布后发现因为没有使用https,谷歌浏览器会在地址栏提示网站不安全.因此想提升下网站的 ...

  5. PHP中的数据库操作

    PDO project data object 连接到数据库 $db=new PDO("mysql:dbname=database;host=sever","userna ...

  6. (第二篇)shell的简单了解

    Shell (类似开发工具) Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁.Shell 既是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程 ...

  7. java中ThreadLocal的使用

    文章目录 在Map中存储用户数据 在ThreadLocal中存储用户数据 java中ThreadLocal的使用 ThreadLocal主要用来为当前线程存储数据,这个数据只有当前线程可以访问. 在定 ...

  8. Scala教程之:可扩展的scala

    文章目录 隐式类 限制条件 字符串插值 s 字符串插值器 f 插值器 raw 插值器 自定义插值器 Scala是扩展的,Scala提供了一种独特的语言机制来实现这种功能: 隐式类: 允许给已有的类型添 ...

  9. js 运动函数篇(二) (加速度运动、弹性运动、重力场运动(多方向+碰撞检测+重力加速度+能量损失运动)拖拽运动)层层深入

    前言:         本人纯小白一个,有很多地方理解的没有各位大牛那么透彻,如有错误,请各位大牛指出斧正!小弟感激不尽.         本篇文章为您分析一下原生JS写加速度运动.弹性运动.重力场运 ...

  10. 【linux题目】第二关

    1.创建目录/data/oldboy,并且在该目录下创建文件oldboy.txt,然后在文件oldboy.txt里写入内容”inet addr:10.0.0.8 Bcast:10.0.0.255 Ma ...