java 最长回文字串
- package string.string1_6;
- public class LongestPalidrome
- {
- /**
- * 使用常规方法, 以字符串的每一个字符作为中心进行判断, 包括奇数和偶数的情况
- * 最坏时间复杂度为O(N^2) , 空间复杂度O(1)
- */
- public static int longestPalidrome(String s)
- {
- if(s == null || s.length() <= 0)
- return 0 ;
- int max = 0 ;
- for(int i=0 ; i<s.length() ; i++)
- {
- for(int time=0 ; time<2 ; time++)
- {
- int length = getMax(s , i , time+i) ;
- if(max < length)
- max = length ;
- }
- }
- return max ;
- }
- private static int getMax(String s, int left , int right) {
- while (left >= 0 && right <= s.length() - 1) {
- if (s.charAt(left) != s.charAt(right))
- break;
- left--;
- right++;
- }
- return right - left - 1;
- }
- /**
- * 使用manacher算法(其实就是动态规划的一种情况)
- * 时间复杂度O(N), 空间复杂度O(N)
- */
- public static int longestPalidrome2(String s)
- {
- if(s == null || s.length() < 1)
- return 0 ;
- String judge = init(s) ;
- int id = 0 ; //最大回文的中心
- int mx = 0 ; //为id+p[id], 也就是最大回文的后半段
- int[] p = new int[judge.length()] ;
- for(int i=1 ; i<judge.length()-1 ; i++)
- {
- if(i < mx)
- {
- p[i] = min(p[2*id-i] , mx-i) ; //对p[i]进行预测
- }
- else
- {
- p[i] = 1 ; //无法使用之前的结论进行预测, 因此只能先假设p[i]只为该元素本身的长度1.
- }
- while (judge.charAt(i-p[i]) == judge.charAt(i+p[i])) //在mx外是否仍存在回文
- p[i]++ ;
- //更新mx和id
- if(p[i]+i-1 > mx)
- {
- mx = p[i] + i -1;
- id = i ;
- }
- }
- int mxId = 0 ;
- for(int i=1 ; i<p.length ; i++)
- {
- if(p[i] > p[mxId])
- mxId = i ;
- }
- return p[mxId]-1 ;
- }
- /**
- * 预处理:将原来的字符串str填充为为$#...#*的格式
- * 由于java没有\0作为字符串的结束标志, 因此在结尾使用*作为结束标志
- * 其中的$和*是用来当哨兵的
- */
- private static String init(String str)
- {
- StringBuilder sb = new StringBuilder(str.length()*2+2) ;
- sb.append("$#") ;
- for(int i=0 ; i<str.length() ; i++)
- {
- sb.append(str.charAt(i)).append("#") ;
- }
- sb.append("*") ;
- return sb.toString() ;
- }
- private static int min(int a , int b)
- {
- return a > b ? b : a ;
- }
- public static void main(String[] args)
- {
- //dsjfkldsababasdlkfjsd
- //skjflkdsjfkldsababasdlkfjsdwieowowwpw
- String str = "a";
- System.out.println(longestPalidrome2(str));
- }
- }
参考://https://yq.aliyun.com/articles/3739
java 最长回文字串的更多相关文章
- 求字符串的最长回文字串 O(n)
昨天参加了某公司的校园招聘的笔试题,做得惨不忍睹,其中就有这么一道算法设计题:求一个字符串的最长回文字串.我在ACM校队选拔赛上遇到过这道题,当时用的后缀数组AC的,但是模板忘了没写出代码来. 回头我 ...
- hihocoder 第一周 最长回文字串
题目1 : 最长回文子串 时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程 ...
- POJ 3974 最长回文字串(manacher算法)
题意:给出一个字符串,求出最长回文字串. 思路:一开始我直接上了后缀数组DC3的解法,然后MLE了.看了DISCUSS发现还有一种计算回文字串更加优越的算法,就是manacher算法.就去学习了一下, ...
- 最长回文字串——manacher算法
时间复杂度:O(n) 参考:https://segmentfault.com/a/1190000003914228 1.问题定义 最长回文子串问题:给定一个字符串,求它的最长回文子串长度. 如果一个字 ...
- Hdu 3068 最长回文字串Manacher算法
题目链接 最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 最长回文字串 (The longest palindrome substring)
这两天去学了一下,觉得下面那篇文章写的很好,有例子,比较容易懂,所以转一下. 以下内容来自:hihoCoder: 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互 ...
- Leetcode5.Longest Palindromic Substring最长回文字串
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &quo ...
- 【POJ3974】最长回文字串
在这里采用的是哈希+二分的方法. 根据回文串的性质可知,可以将回文分成奇回文和偶回文分别进行处理. 对于奇回文来说,每次枚举的端点一定是重合的,因此只需计算出端点左右公共的长度是多少即可,因此二分的是 ...
- 【LeetCode每天一题】Longest Palindromic Substring(最长回文字串)
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
随机推荐
- java面试题之volatile的工作原理
volatile的特性: volatile可见性:对一个volatile的读,总可以看到对这个变量最终的写: volatile原子性:volatile对单个读/写具有原子性(32位Long.Doubl ...
- bzoj1433 [ZJOI2009]假期的宿舍 最大流
[ZJOI2009]假期的宿舍 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3429 Solved: 1459[Submit][Status][D ...
- bzoj1143/2718 祭祀river(最大独立集)
[CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2175 Solved: 1098[Submit][Status] ...
- jenkins发送html测试报告
jenkins发送html测试报告 https://blog.csdn.net/galen2016/article/details/77975965/ <!DOCTYPE html> & ...
- hust 1605 - Gene recombination(bfs+字典树)
1605 - Gene recombination Time Limit: 2s Memory Limit: 64MB Submissions: 264 Solved: 46 DESCRIPTION ...
- [转]使用ProxyFactoryBean创建AOP代理
http://doc.javanb.com/spring-framework-reference-zh-2-0-5/ 7.5. 使用ProxyFactoryBean创建AOP代理 - Spring F ...
- 标准C程序设计七---57
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- js-压缩混淆工具 uglifyjs
单个打包文件npm install uglify-js -g 使用uglifyjs压缩js uglifyjs 原始js文件 -m -c -o 压缩后js文件 uglifyjs 原始js文件 -b -c ...
- react css module
<div className={style['content-warp']}></div> <div className={style.search}></d ...
- Codeforces 599E Sandy and Nuts(状压DP)
题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...