(manacher 1.1)hdu 3068回文(使用manacher判断回文简单题)

小鱼儿 2022-03-01 06:06 246阅读 0赞

最长回文

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 33140 Accepted Submission(s): 12132

Problem Description

给出一个只由小写英文字符a,b,c…y,z组成的字符串S,求S中最长回文串的长度.
回文就是正反读都是一样的字符串,如aba, abba等

Input

输入有多组case,不超过120组,每组输入为一行小写英文字符a,b,c…y,z组成的字符串S
两组case之间由空行隔开(该空行不用处理)
字符串长度len <= 110000

Output

每一行一个整数x,对应一组case,表示该组case的字符串中所包含的最长回文长度.

Sample Input

aaaa abab

Sample Output

4 3

Source

2009 Multi-University Training Contest 16 - Host by NIT

Recommend

lcy | We have carefully selected several similar problems for you: 3336 3065 3067 3063 3064

  1. #include <vector>
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdio>
  5. using namespace std;
  6. const int MAXN = 100005;
  7. //string Manacher(string s) {
  8. // // Insert '#'
  9. // string t = "$#";
  10. // for (int i = 0; i < s.size(); ++i) {
  11. // t += s[i];
  12. // t += "#";
  13. // }
  14. //
  15. // // Process t
  16. // vector<int> p(t.size(), 0);
  17. //
  18. //
  19. // int mx = 0, id = 0, resLen = 0, resCenter = 0;
  20. // for (int i = 1; i < t.size(); ++i) {
  21. //
  22. // //找到回文串的长度
  23. // p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
  24. //
  25. // /*
  26. // * 尝试拓宽边界
  27. // */
  28. // while (t[i + p[i]] == t[i - p[i]]) {
  29. // ++p[i];
  30. // }
  31. //
  32. // /*
  33. // * 更新回文串的边界
  34. // */
  35. // if (mx < i + p[i]) {
  36. // //更新回文串右边的边界
  37. // mx = i + p[i];
  38. // //更新回文串的中心
  39. // id = i;
  40. // }
  41. //
  42. // //更新回文串的中心数据
  43. // if (resLen < p[i]) {
  44. //
  45. // //回文串的半径(就是处理后的回文串)
  46. // resLen = p[i];
  47. // //回文串的中心
  48. // resCenter = i;
  49. //
  50. // }
  51. // }
  52. //
  53. // /*
  54. // * s是原来的文本串.
  55. // *
  56. // * resLen:处理后的回文串的半径,但是是处理前的回文串的直径
  57. // * resCenter:处理后的回文串的中心
  58. // *
  59. // * resCenter - resLen:计算回文串的起点
  60. // * resLen - 1:需要
  61. // */
  62. // return s.substr((resCenter - resLen) / 2, resLen - 1);
  63. //}
  64. string manacher(string s) {
  65. int mx = 0;
  66. int id = 0;
  67. int resCenter = 0;
  68. int resLen = 0;
  69. string t = "$#";
  70. for (int i = 0; i < s.length(); ++i) {
  71. t += s[i];
  72. t += "#";
  73. }
  74. vector<int> p(t.length(), 0);
  75. for (int i = 0; i < t.length(); ++i) {
  76. p[i] = mx > i ? min(p[2 * id - i], mx - i) : 1;
  77. while (t[i + p[i]] == t[i - p[i]]) {
  78. p[i] += 1;
  79. }
  80. if (mx < i + p[i]) {
  81. mx = i + p[i];
  82. id = i;
  83. }
  84. if (resLen < p[i]) {
  85. resLen = p[i];
  86. resCenter = i;
  87. }
  88. }
  89. return s.substr((resCenter - resLen) / 2, resLen - 1);
  90. }
  91. int main() {
  92. // string s1 = "12212";
  93. // cout << Manacher(s1) << endl;
  94. // string s2 = "122122";
  95. // cout << Manacher(s2) << endl;
  96. // string s = "waabwswfd";
  97. // cout << Manacher(s) << endl;
  98. //
  99. //
  100. // cout << "----------" << endl;
  101. //
  102. // cout << manacher(s1) << endl;
  103. // string s22 = "122122";
  104. // cout << manacher(s22) << endl;
  105. // string s33 = "waabwswfd";
  106. // cout << manacher(s33) << endl;
  107. // string tmp = "";
  108. char tmp[MAXN];
  109. while (scanf("%s", tmp) != EOF) {
  110. string result = manacher(tmp);
  111. cout << result.length() << endl;
  112. }
  113. }

发表评论

表情:
评论列表 (有 0 条评论,246人围观)

还没有评论,来说两句吧...

相关阅读