1.  
  1. package string.string1_6;
  2.  
  3. public class LongestPalidrome
  4. {
  5. /**
  6. * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶数的情况
  7. * 最坏时间复杂度为O(N^2) , 空间复杂度O(1)
  8. */
  9. public static int longestPalidrome(String s)
  10. {
  11. if(s == null || s.length() <= 0)
  12. return 0 ;
  13.  
  14. int max = 0 ;
  15.  
  16. for(int i=0 ; i<s.length() ; i++)
  17. {
  18. for(int time=0 ; time<2 ; time++)
  19. {
  20. int length = getMax(s , i , time+i) ;
  21. if(max < length)
  22. max = length ;
  23. }
  24. }
  25.  
  26. return max ;
  27. }
  28.  
  29. private static int getMax(String s, int left , int right) {
  30. while (left >= 0 && right <= s.length() - 1) {
  31. if (s.charAt(left) != s.charAt(right))
  32. break;
  33. left--;
  34. right++;
  35. }
  36.  
  37. return right - left - 1;
  38. }
  39.  
  40. /**
  41. * 使用manacher算法(其实就是动态规划的一种情况)
  42. * 时间复杂度O(N), 空间复杂度O(N)
  43. */
  44. public static int longestPalidrome2(String s)
  45. {
  46. if(s == null || s.length() < 1)
  47. return 0 ;
  48. String judge = init(s) ;
  49.  
  50. int id = 0 ; //最大回文的中心
  51. int mx = 0 ; //为id+p[id], 也就是最大回文的后半段
  52. int[] p = new int[judge.length()] ;
  53.  
  54. for(int i=1 ; i<judge.length()-1 ; i++)
  55. {
  56. if(i < mx)
  57. {
  58. p[i] = min(p[2*id-i] , mx-i) ; //对p[i]进行预测
  59. }
  60. else
  61. {
  62. p[i] = 1 ; //无法使用之前的结论进行预测, 因此只能先假设p[i]只为该元素本身的长度1.
  63. }
  64.  
  65. while (judge.charAt(i-p[i]) == judge.charAt(i+p[i])) //在mx外是否仍存在回文
  66. p[i]++ ;
  67.  
  68. //更新mx和id
  69. if(p[i]+i-1 > mx)
  70. {
  71. mx = p[i] + i -1;
  72. id = i ;
  73. }
  74. }
  75.  
  76. int mxId = 0 ;
  77. for(int i=1 ; i<p.length ; i++)
  78. {
  79. if(p[i] > p[mxId])
  80. mxId = i ;
  81. }
  82.  
  83. return p[mxId]-1 ;
  84. }
  85.  
  86. /**
  87. * 预处理:将原来的字符串str填充为为$#...#*的格式
  88. * 由于java没有\0作为字符串的结束标志, 因此在结尾使用*作为结束标志
  89. * 其中的$和*是用来当哨兵的
  90. */
  91. private static String init(String str)
  92. {
  93. StringBuilder sb = new StringBuilder(str.length()*2+2) ;
  94.  
  95. sb.append("$#") ;
  96. for(int i=0 ; i<str.length() ; i++)
  97. {
  98. sb.append(str.charAt(i)).append("#") ;
  99. }
  100.  
  101. sb.append("*") ;
  102. return sb.toString() ;
  103. }
  104. private static int min(int a , int b)
  105. {
  106. return a > b ? b : a ;
  107. }
  108.  
  109. public static void main(String[] args)
  110. {
  111. //dsjfkldsababasdlkfjsd
  112. //skjflkdsjfkldsababasdlkfjsdwieowowwpw
  113. String str = "a";
  114.  
  115. System.out.println(longestPalidrome2(str));
  116. }
  117. }

参考://https://yq.aliyun.com/articles/3739

java 最长回文字串的更多相关文章

  1. 求字符串的最长回文字串 O(n)

    昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...

  2. hihocoder 第一周 最长回文字串

    题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...

  3. POJ 3974 最长回文字串(manacher算法)

    题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...

  4. 最长回文字串——manacher算法

    时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...

  5. Hdu 3068 最长回文字串Manacher算法

    题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  6. 最长回文字串 (The longest palindrome substring)

    这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...

  7. Leetcode5.Longest Palindromic Substring最长回文字串

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...

  8. 【POJ3974】最长回文字串

    在这里采用的是哈希+二分的方法. 根据回文串的性质可知,可以将回文分成奇回文和偶回文分别进行处理. 对于奇回文来说,每次枚举的端点一定是重合的,因此只需计算出端点左右公共的长度是多少即可,因此二分的是 ...

  9. 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

随机推荐

  1. java面试题之volatile的工作原理

    volatile的特性: volatile可见性:对一个volatile的读,总可以看到对这个变量最终的写: volatile原子性:volatile对单个读/写具有原子性(32位Long.Doubl ...

  2. bzoj1433 [ZJOI2009]假期的宿舍 最大流

    [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3429  Solved: 1459[Submit][Status][D ...

  3. bzoj1143/2718 祭祀river(最大独立集)

    [CTSC2008]祭祀river Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2175  Solved: 1098[Submit][Status] ...

  4. jenkins发送html测试报告

    jenkins发送html测试报告  https://blog.csdn.net/galen2016/article/details/77975965/ <!DOCTYPE html> & ...

  5. hust 1605 - Gene recombination(bfs+字典树)

    1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...

  6. [转]使用ProxyFactoryBean创建AOP代理

    http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ 7.5. 使用ProxyFactoryBean创建AOP代理 - Spring F ...

  7. 标准C程序设计七---57

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  8. js-压缩混淆工具 uglifyjs

    单个打包文件npm install uglify-js -g 使用uglifyjs压缩js uglifyjs 原始js文件 -m -c -o 压缩后js文件 uglifyjs 原始js文件 -b -c ...

  9. react css module

    <div className={style['content-warp']}></div> <div className={style.search}></d ...

  10. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...