最长回文子串(Longest Palindromic Substring)-DP问题
问题描述:
给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,而且存在唯一的最长回文子串 。
思路分析:
动态规划的思路:dp[i][j] 表示的是 从i 到 j 的字串,是否是回文串。
则根据回文的规则我们可以知道:
如果s[i] == s[j] 那么是否是回文决定于 dp[i+1][ j - 1]
当 s[i] != s[j] 的时候, dp[i][j] 直接就是 false。
动态规划的进行是按照字符串的长度从1 到 n推进的。
DP算法实现:
package com.ysw.test; import java.util.Scanner; /*
* 问题描述:
* 给定一个字符串S,找出它的最大的回文子串,你可以假设字符串的最大长度是1000,
* 而且存在唯一的最长回文子串 。
*/
public class LongestPalindrome { /**
* @param args
*/
public static void main(String[] args) {
// 从键盘读入字符串
String str = null;
Scanner reader = new Scanner(System.in);
str = reader.nextLine();
System.out.println(getLongestPalindrome(str));
} /**
* 此方法返回s的最长回文串
*
* @param str
* @return
*/
private static String getLongestPalindrome(String str) { boolean dp[][];
// 如果字符串的长度为0,则认为str的最长回文串为空字符串
if (str.length() == 0) {
return "";
}
// 字符串str长度为1.则字符串本身就是一个最长回文串
if (str.length() == 1) {
return str;
}
// dp[i][j],表示字符串str从str[i]到str[j]的子串为最长回文子串
dp = new boolean[str.length()][str.length()];
// 记录已经找到的最长回文子串的长度
int maxLen = 1;
// 记录最长回文子串的起点位置和终点位置
int start = 0, end = 0;
// 动态规划的进行是按照字符串的长度从1 到 n推进的,k表示正在判断的子串的长度
// 用于和已知的子串的长度maxLen进行比较
int k;
// 找出str的所有子串的dp对应的boolean值,初始化过程
for (int i = 0; i < str.length(); i++) {
for (int j = 0; j < str.length(); j++) {
// 当i==j的时候,只有一个字符的字符串
// 当i>j的时候认为是空串,dp[i][j]
if (i >= j) {
dp[i][j] = true;
} else {
dp[i][j] = false;
}
}
} // 我在这里犯了一个幼稚的错误,把i、j的定义放在了for循环中,在else{}中是访问不到的
// 运行程序报java.lang.StringIndexOutOfBoundsException: String index out of
// range错误
int i, j;
for (k = 1; k < str.length(); k++) {
for (i = 0; i + k < str.length(); i++) { j = i + k;
if (str.charAt(i) != str.charAt(j)) {
dp[i][j] = false;
} else {
dp[i][j] = dp[i + 1][j - 1];
if (dp[i][j]) {
// 判断找到的子串的长度是否大于我们已知的最长子串的长度
if (k + 1 > maxLen) {
// 记录最长回文子串
maxLen = k + 1;
// 记录子串的起始位置,因为后面的函数subString(int beginIndex,int
// endIndex)函数要用到
start = i;
end = j;
}
}
}
}
}
return str.substring(start, end + 1);
}
}
【注意】:函数subString返回一个新字符串,它是此字符串的一个子字符串。该子字符串从指定的 beginIndex 处开始,直到索引 endIndex - 1 处的字符。因此,该子字符串的长度为 endIndex-beginIndex。
最长回文子串(Longest Palindromic Substring)-DP问题的更多相关文章
- [译+改]最长回文子串(Longest Palindromic Substring) Part II
[译+改]最长回文子串(Longest Palindromic Substring) Part II 原文链接在http://leetcode.com/2011/11/longest-palindro ...
- [译]最长回文子串(Longest Palindromic Substring) Part I
[译]最长回文子串(Longest Palindromic Substring) Part I 英文原文链接在(http://leetcode.com/2011/11/longest-palindro ...
- 领扣-5 最长回文子串 Longest Palindromic Substring MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- [Swift]LeetCode5. 最长回文子串 | Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...
- LeetCode.5-最长回文子串(Longest Palindromic Substring)
这是悦乐书的第342次更新,第366篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第3题(顺位题号是5).给定一个字符串s,找到s中最长的回文子字符串. 您可以假设s ...
- 【算法】最长回文子串 longest palindrome substring
对于字符串S, 要找到它最长的回文子串,能想到的最暴力方法,应该是对于每个元素i-th都向左向右对称搜索,最后用一个数组span 记录下相对应元素i-th为中心的回文子串长度. 那么问题来了: 1. ...
- [Swift]LeetCode516. 最长回文子序列 | Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- LeetCode:Longest Palindromic Substring 最长回文子串
题目链接 Given a string S, find the longest palindromic substring in S. You may assume that the maximum ...
- 转载:LeetCode:5Longest Palindromic Substring 最长回文子串
本文转自:http://www.cnblogs.com/TenosDoIt/p/3675788.html 题目链接 Given a string S, find the longest palindr ...
随机推荐
- cololection
package cn.bjsxt.col; /** * 简化迭代器原理 * hasNext * next * @author Administrator * */ public class MyArr ...
- Html,Css,Javascript及其他的注释方法详解
一.HTML的注释方法<!-- html注释:START -->内容<!-- html注释:END --> 包含在“<!--”与“-->”之间的内容将会被浏览器忽略 ...
- Objective-C命名编写规范
There are only two hard things in Computer Science: cache invalidation and naming things. 在计算机科学中只有两 ...
- $.post()
定义和用法 post() 方法通过 HTTP POST 请求从服务器载入数据. jQuery.post(url,data,success(data, textStatus, jqXHR),dataTy ...
- wince和window mobile winphone
windows mobile是微软在2000年左右推出的针对移动平台的操作系统,这个系统一直使用到三年前,微软开始启用metro界面,将windows mobile改名为windows phone. ...
- 将多个.a库合并为一个.a库的方法
如果编译了多个架构的静态库,想将它们合并为一个静态库的时候,可以用如下方法合并: sudo lipo -create /libs/ffmpeg/2.6.3/arm64/lib/libavcodec.a ...
- Grunt + Bower—前端构建利器(转)
目前比较流行的WEB开发的趋势是前后端分离.前端采用重量级的Javascript框架,比如Angular,Ember等,后端采用restful API的Web Service服务,通过JSON格式进行 ...
- 如何使用 orachk 工具
Oracle RAC 安装完毕后的健壮性是一个令人头疼的问题.之前Oracle为之专门推出了raccheck工具,确实方便了我们这些个苦逼的DBA.现在Oracle在raccheck的基础之上又推出了 ...
- 【转】AVL
#include <iostream> #include <ctime> #include <queue> #include <cassert> #in ...
- 【转】从INF文件认识驱动
在工控机安装xp操作系统时,由于工控机的集成显卡驱动只支持win7,之前没接触过windows驱动相关内容,折腾了半天.下载的驱动是exe的,双击安装就提示安装失败(未签名) 上图是网上随便找的,现象 ...