我正在开发一个回文最长的程序:
这是我的代码,这对于最长的子序列工作正常。但是我想用另一种方式来做,例如:
@media print{
.content {display:block;}
}
,我希望结果为havanbava
,但是我的程序给了我avanava
。如何解决这个问题。
ava
答案 0 :(得分:2)
考虑到有两个不同的最长回文子序列( avanava 和 avabava ),您可以迭代地找到所有子序列,然后检查它们是否是回文。 我使用地图保存所有回文序列及其长度,然后在地图中循环选择最长的子序列。 此解决方案仅采用第一个最长的((<< >> avanava ):
import java.util.HashMap;
import java.util.Map;
public class Palindrome {
// set to store all the subsequences
static Map<String, Integer> subsequences = new HashMap<>();
public static void main(String[] args) {
subsequence("havanbava");
//storing the higher key/value
Map.Entry<String, Integer> maxEntry = null;
for (Map.Entry<String, Integer> entry : subsequences.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}
System.out.println(maxEntry.getKey());
}
static boolean isPalindrome(String str) {
int n = str.length();
for (int i = 0; i < (n/2); ++i) {
if (str.charAt(i) != str.charAt(n - i - 1)) {
return false;
}
}
return true;
}
static void subsequence(String str)
{
for (int i = 0; i < str.length(); i++) {
for (int j = str.length(); j > i; j--) {
String sub_str = str.substring(i, j);
if (!subsequences.containsKey(sub_str)
&& isPalindrome(sub_str))
subsequences.put(sub_str,sub_str.length());
for (int k = 1; k < sub_str.length() - 1; k++) {
StringBuffer sb = new StringBuffer(sub_str);
sb.deleteCharAt(k);
subsequence(sb.toString());
}
}
}
}
}
还要考虑到迭代解决方案会消耗资源,因此对于长字符串来说,它需要进行一些重大改进。
如果要所有最长的子序列(相同长度),这是一种可能的解决方案:
import java.util.HashMap;
import java.util.Map;
public class Palindrome {
// set to store all the subsequences
static Map<String, Integer> subsequences = new HashMap<>();
public static void main(String[] args) {
subsequence("havanbava");
//storing the higher key/value
Map<String, Integer> maxEntries = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : subsequences.entrySet())
{
if (maxEntries.isEmpty()){
maxEntries.put(entry.getKey(),entry.getValue());
}else if(entry.getValue().compareTo( maxEntries.entrySet().iterator().next().getValue() ) == 0)
{
maxEntries.put(entry.getKey(),entry.getValue());
}else if( entry.getValue().compareTo(maxEntries.entrySet().iterator().next().getValue()) > 0){
maxEntries.clear();
maxEntries.put(entry.getKey(),entry.getValue());
}
}
for (Map.Entry<String, Integer> maxEntry : maxEntries.entrySet())
System.out.println(maxEntry.getKey());
}
static boolean isPalindrome(String str) {
int n = str.length();
for (int i = 0; i < (n/2); ++i) {
if (str.charAt(i) != str.charAt(n - i - 1)) {
return false;
}
}
return true;
}
static void subsequence(String str)
{
for (int i = 0; i < str.length(); i++) {
for (int j = str.length(); j > i; j--) {
String sub_str = str.substring(i, j);
if (!subsequences.containsKey(sub_str)
&& isPalindrome(sub_str))
subsequences.put(sub_str,sub_str.length());
for (int k = 1; k < sub_str.length() - 1; k++) {
StringBuffer sb = new StringBuffer(sub_str);
sb.deleteCharAt(k);
subsequence(sb.toString());
}
}
}
}
}
答案 1 :(得分:1)
Тhis是一个典型的dynamic programming问题。您可以在O(n^2)
的时间内找到最长回文序列的子序列(LPS)的长度。您应该像这样构造一个记忆矩阵:
0 1 2 3 4 5 6 7 8 -> indexes
h a v a n b a v a -> input string
0 h 1 1 1 3 3 3 3 5 7 -> max_len = 7 -> a v a b a v a
1 a 0 1 1 3 3 3 3 5 7
2 v 0 0 1 1 1 1 3 5 5
3 a 0 0 0 1 1 1 3 3 3
4 n 0 0 0 0 1 1 1 1 3
5 b 0 0 0 0 0 1 1 1 3
6 a 0 0 0 0 0 0 1 1 3
7 v 0 0 0 0 0 0 0 1 1
8 a 0 0 0 0 0 0 0 0 1
想法是构造一个矩阵,使得matrix[i][j]
(0 <= i,j <= len)等于输入的第i个索引到第j个索引的LPS。
如果第i
至第j
索引(i<=j
)之间的LPS为LPS(i, j)
,长度为L
:
L=1
,我们有:
LPS(0,0) = LPS(1,1) = ... = LPS(8,8) = 1
L=2
:LPS(0,1) = LPS(1,2) = ... = LPS(7,8) = 1
(如果input[i]=input[i+1]
,LPS(i,i+1)=2
(即aa
或bb
,但我们没有这种情况)< / li>
L=3
,我们有:
LPS(0,2) = max(LPS(0,1), LPS(1,2)) = max(1, 1) = 1
LPS(1,3) = LPS(1,1) + 2 = 3
更多示例:
LPS(0,4) = max(LPS(0,3), LPS(1,4))=max(3,2) = 3
LPS(2,7) = LPS(3,6) + 2 = 3 + 2 = 5
所以规则是:
if input[i] != input[j]
LPS(i,j) = max(LPS(i,j-1), LPS(i+1,j))
else
LPS(i,j) = LPS(i+1,j-1) + 2
您可以找到有关LPS here的精彩解释。这个家伙讲得很好,我强烈推荐他的动态编程播放列表。当然,这个问题可以通过递归解决方案来解决(就像每个DP问题一样),但是在这里我建议一个示例DP解决方案:
public static String findLPS(String input) {
int len = input.length();
// initializes a diagonal matrix
int[][] matrix = new int[len][len];
for (int i = 0; i < matrix.length; i++) {
matrix[i][i] = 1;
}
// finds the length of the longest palindrome subsequence
for (int jj = 1; jj < len; jj++) {
int i = 0;
int j = jj;
while (i < len && j < len) {
if (input.charAt(i) == input.charAt(j)) {
matrix[i][j] = matrix[i + 1][j - 1] + 2;
} else {
matrix[i][j] = Math.max(matrix[i + 1][j], matrix[i][j - 1]);
}
i++; j++;
}
}
// reconstruct the solution from the matrix
char[] path = new char[len];
int i = 0;
int j = len - 1;
if (matrix[i][j] == 1) {
return input.charAt(0) + "";
}
while (matrix[i][j] != 0) {
if (matrix[i][j] == matrix[i + 1][j]) {
i += 1;
} else if (matrix[i][j] == matrix[i][j - 1]) {
j -= 1;
} else {
path[i] = input.charAt(i);
path[j] = input.charAt(j);
i++; j--;
}
}
String solution = "";
for (int k = 0; k < len; k++) {
if(path[k] != 0) {
solution += path[k];
}
}
return solution;
}