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
12321
最长字符串长度:5 我爱你爱我
我爱你爱我
最长字符串长度:5 我爱她

最长字符串长度:1

Java实现最长回文串的更多相关文章

  1. 算法笔记_032:最长回文串(Java)

    目录 1 问题描述 2 解决方案 2.1 中心扩展法 2.2 Manacher算法   1 问题描述 给定一个字符串,求它的最长回文子串的长度. 2 解决方案 2.1 中心扩展法 此处,首先枚举出回文 ...

  2. Java实现 LeetCode 409 最长回文串

    409. 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意 ...

  3. (最长回文串 模板) 最长回文 -- hdu -- 3068

    http://acm.hdu.edu.cn/showproblem.php?pid=3068 最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory ...

  4. Manacher(输出最长回文串及下标)

    http://acm.hdu.edu.cn/showproblem.php?pid=3294 Girls' research Time Limit: 3000/1000 MS (Java/Others ...

  5. Manacher算法 - 求最长回文串的利器

    求最长回文串的利器 - Manacher算法 Manacher主要是用来求某个字符串的最长回文子串. 不要被manacher这个名字吓倒了,其实manacher算法很简单,也很容易理解,程序短,时间复 ...

  6. ACM题目————最长回文串

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

  7. MANACHER---求最长回文串

    求最长回文串,如果是暴力的方法的话,会枚举每个字符为中心,然后向两边检测求出最长的回文串,时间复杂度在最坏的情况下就是0(n^2),为什么时间复杂度会这么高,因为对于每一个作为中心的字符的检测是独立的 ...

  8. 字符串的最长回文串:Manacher’s Algorithm

    题目链接:Longest Palindromic Substring 1. 问题描述 Given a string S, find the longest palindromic substring ...

  9. Manacher's Algorithm 马拉车算法(求最长回文串)

    作用:求一个字符串中的最长子串,同时还可以求所有子串的长度. 题目链接: https://vjudge.net/contest/254692#problem/B 最长回文串长度的代码: int Man ...

随机推荐

  1. SQL SERVER 的窗体函数OVER的使用:row_number/rank/dense_rank

    举个例子给大家加深印象,也方便理解: 1.目前有这几笔数据: Select as score into #studentSoure union all Select as score union al ...

  2. at命令用法详解

    在linux系统中你可能已经发现了为什么系统常常会自动的进行一些任务?这些任务到底是谁在支配他们工作的? 在linux系统如果你想要让自己设计的备份程序可以自动在某个时间点开始在系统底下运行,而不需要 ...

  3. python -使用Requests库完成Post表单操作

    """ 使用Requests库完成Post表单操作 """ #_*_codingn:utf8 _*_ import requests fro ...

  4. java中字符串截取

    1.使用StringUtils,需要导包 String strs = "abcdef1003535197"; System.out.println("=====2==== ...

  5. JDBC基本使用方法

    JDBC基本使用方法 JDBC固定步骤: 加载驱动 String url="jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true& ...

  6. 牛客网挑战赛19 B,C,F

    链接:https://www.nowcoder.com/acm/contest/131/B来源:牛客网 矩阵 M 包含 R 行 C 列,第 i 行第 j 列的值为 Mi,j. 请寻找一个子矩阵,使得这 ...

  7. html5拖动监听

    在拖动目标上触发事件 (源元素): ondragstart - 用户开始拖动元素时触发 ondrag - 元素正在拖动时触发 ondragend - 用户完成元素拖动后触发 释放目标时触发的事件: o ...

  8. 《HelloGitHub》第 50 期

    兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...

  9. Java 获取IP工具类、Vo类整理记录

    前言 日常开发中,获取ip是常用的功能,本文记录如何在Java中获取本机外网ip.地理位置,访问用户的外网ip.地理位置,以及指定外网ip的地理位置: 代码编写 1.获取访问用户外网ip,我们从访问者 ...

  10. 极客手中的利器Electron

    作为一个前端开发人员,你可能已经听说过Electron了,你知道VS Code是基于这个技术开发的.不但VS Code, 目前一些大热的软件:飞书.Slack.WhatsApp都是基于这个技术开发的. ...