算法笔记_032:最长回文串(Java)
目录
1 问题描述
给定一个字符串,求它的最长回文子串的长度。
2 解决方案
2.1 中心扩展法
此处,首先枚举出回文串的中心位置,然后,再在该位置上分别向左和向右扩展,记录并更新得到的最长回文串的长度。
具体代码如下:
package com.liuzhen.string_1;
import java.util.Scanner;
public class StringLongestPalindrome {
/*
* 参数A:给定字符串
* 函数功能:返回字符串A中最长回文串的长度
*/
public int getLongestPalindrome(String A){
char[] arrayA = A.toCharArray();
int max = 0;
int tempMax = 0;
if(A.equals("") || A.equals(null))
return 0;
for(int i = 0;i < arrayA.length;i++){ //i为回文串的中心位置
//当回文串位数为奇数时
for(int j = 0;(i-j) >= 0 && (i+j) < arrayA.length;j++){
if(arrayA[i-j] != arrayA[i+j])
break;
tempMax = 2*j + 1;
}
if(tempMax > max)
max = tempMax;
//当回文串位数为偶数时
for(int j = 0;(i-j) >= 0 && (i+j+1) < arrayA.length;j++){
if(arrayA[i-j] != arrayA[i+j+1])
break;
tempMax = 2*j + 2;
}
if(tempMax > max)
max = tempMax;
}
return max;
}
public static void main(String[] args){
StringLongestPalindrome test = new StringLongestPalindrome();
Scanner in = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String A = in.nextLine();
int maxA = test.getLongestPalindrome(A);
System.out.println("输入目标字符串中最长回文串的长度为:"+maxA);
}
}
运行结果:
请输入一个字符串:
abba
输入目标字符串中最长回文串的长度为:4 请输入一个字符串:
aabbbbba
输入目标字符串中最长回文串的长度为:7 请输入一个字符串:
我爱爱我我我啊
输入目标字符串中最长回文串的长度为:4
2.2 Manacher算法
package com.liuzhen.practice;
import java.util.Scanner;
public class Main {
public void Manacher(String A) {
StringBuffer s = new StringBuffer("$#");
for(int i = 0;i < A.length();i++) {
s.append(A.charAt(i));
s.append("#");
}
A = s.toString();
int[] P = new int[A.length()];
int mx = 0, id = 0;
for(int i = 1;i < A.length();i++) {
if(mx > i)
P[i] = Math.min(P[2 * id - i], mx - i);
else
P[i] = 1;
while(i + P[i] < A.length() && i - P[i] >= 0 && A.charAt(i + P[i]) == A.charAt(i - P[i])) {
P[i]++;
}
if(P[i] + i > mx) {
mx = i + P[i];
id = i;
}
}
int result = -1;
int i = 0, t = 0;
for(;i < P.length;i++) {
if(P[i] > result) {
result = P[i];
t = i;
}
}
for(int j = t - result + 1;j <= t + result - 1;j++) {
if(A.charAt(j) != '#')
System.out.print(A.charAt(j));
}
System.out.println("\n最长字符串长度:"+(result-1));
}
public static void main(String[] args) {
Main test = new Main();
Scanner in = new Scanner(System.in);
String A = in.next();
test.Manacher(A);
}
}
运行结果:
abba
abba
最长字符串长度:4 12212321
12321
最长字符串长度:5 我爱你爱我
我爱你爱我
最长字符串长度:5 我爱她
我
最长字符串长度:1
算法笔记_032:最长回文串(Java)的更多相关文章
- Manacher's Algorithm 马拉车算法(求最长回文串)
作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...
- leetcode.字符串.409最长回文串-Java
1. 具体题目 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串.在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: 假设 ...
- 算法笔记_181:历届试题 回文数字(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 观察数字:12321,123321 都有一个共同的特征,无论从左到右读还是从右向左读,都是相同的.这样的数字叫做:回文数字. 本题要求你找 ...
- 【Manacher算法】求最长回文串的优秀算法
先贴一下代码~ //by 减维 #include<cstdio> #include<iostream> #include<cstring> #include< ...
- Manacher算法 - 求最长回文串的利器
求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...
- manacher 算法(最长回文串)
manacher算法: 定义数组p[i]表示以i为中心的(包含i这个字符)回文串半径长 将字符串s从前扫到后for(int i=0;i<strlen(s);++i)来计算p[i],则最大的p[i ...
- hdu 3068 最长回文 (Manacher算法求最长回文串)
参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...
- ACM题目————最长回文串
Description 给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度. 回文就是正反读都是一样的字符串,如aba, abba等 Input 输入有多组cas ...
- MANACHER---求最长回文串
求最长回文串,如果是暴力的方法的话,会枚举每个字符为中心,然后向两边检测求出最长的回文串,时间复杂度在最坏的情况下就是0(n^2),为什么时间复杂度会这么高,因为对于每一个作为中心的字符的检测是独立的 ...
随机推荐
- codevs 1392 合并傻子
1392 合并傻子 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个园形操场的四周站着N个傻子,现要将傻子有 ...
- hdu 4284 状态压缩dp
题意: 有N 个点的无向图,要去其中 h个地点做事,做事需要先办理护照,之后可以挣一定数量的钱,知道了一开始有的总钱数,和 一些城市之间 道路的花费,问可不可以在 指定的 h 个城 ...
- http隧道的研究
1.reDuh为什么要bind一个udp,如何维持tcp的? 似乎只要不close,就不会关闭打开过的socket 2.如果web超时,或者脚本超时,是否意味着会断开连接. 似乎并不会 3.是否针对可 ...
- SPFA cojs 176. [USACO Feb07] 奶牛聚会
cojs 176. [USACO Feb07] 奶牛聚会 ★☆ 输入文件:sparty.in 输出文件:sparty.out 简单对比时间限制:3 s 内存限制:16 MB N(1 ≤ ...
- HDU 5656 CA Loves GCD dp
CA Loves GCD 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5656 Description CA is a fine comrade w ...
- CDOJ 1292 卿学姐种花 暴力 分块 线段树
卿学姐种花 题目连接: http://acm.uestc.edu.cn/#/problem/show/1292 Description 众所周知,在喵哈哈村,有一个温柔善良的卿学姐. 卿学姐喜欢和她一 ...
- luoguoj 1598 垂直柱状图 模拟
P1598 垂直柱状图 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.luogu.org/problem/show?pid=1598 ...
- Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words
Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...
- PAT甲级1089. Insert or Merge
PAT甲级1089. Insert or Merge 题意: 根据维基百科: 插入排序迭代,消耗一个输入元素每次重复,并增加排序的输出列表.每次迭代,插入排序从输入数据中删除一个元素,在排序列表中找到 ...
- Android的Master/Detail风格界面中实现自定义ListView的单选
原文在这里:http://duduli.iteye.com/blog/1453576 可以实现多选,那么如何实现单选呢,这里我写了一个非常简单的方法: public void onListItemCl ...