[英]How to find every palindrome substring in a given string and return an occurence value in under a second
我一直在努力解决以下问题:
您将获得一串小写拉丁字母。 让我们将子字符串的“出现值”定义为字符串中子字符串出现次数乘以子字符串的长度。 对于给定的字符串,找到回文子串的最大出现值。
我的代码工作正常,但是,我需要在一秒钟内获得解决方案,输入最多300 000个字符。 我的代码到目前为止如下:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
// initiate a scanner
Scanner in = new Scanner(System.in);
String pal = in.nextLine();
getAllPalindrome(pal);
}
/**
* checks if the given string is a palindrome
*
* @param pal
* @return
*/
public static boolean checkPalindrome(String pal) {
for (int i = 0; i < pal.length() / 2; i++) {
if (pal.charAt(i) != pal.charAt(pal.length() - 1 - i)) {
return false;
}
}
return true;
}
/**
* gets all palindromes
*
* @param pal
*/
public static void getAllPalindrome(String pal) {
// initiate variables
ArrayList<String> pals = new ArrayList<String>();
int count = 0;
// add all palindromes to an arraylist
for (int i = 0; i < pal.length(); i++) {
for (int j = i; j < pal.length(); j++) {
if (checkPalindrome(pal.substring(i, j + 1))) {
pals.add(pal.substring(i, j + 1));
}
}
}
int[] counts = new int[pals.size()];
for (int i = 0; i < pals.size(); i++) {
int lCount = 0;
String j = pals.get(i);
for (int k = 0; k < pals.size(); k++) {
if (j.equals(pals.get(k))) {
lCount += 1;
}
counts[i] = lCount * pals.get(i).length();
}
}
int hov = 0;
for (int i = 0; i < pals.size(); i++) {
if (counts[i] > hov) {
hov = counts[i];
}
}
System.out.println(hov);
}
}
以下是速度提升的一些建议:
您正在创建比实际需要更多的String对象。 每次调用substring时,都会创建一个新的String。
您正在存储并因此处理比您需要的更多String对象的分数。 您可以使用一个Set ...或更好的Map而不是ArrayList,其中Entry还包含一个Score。
考虑如何找到以某个位置为中心的每个回文可能比从一个位置开始找到每个回文更有效
考虑如何消除不可能超过当前高分的项目的处理。 (提示:使用对称属性,如“Manacher算法”)。
当您的输入长度为几十万时,您可以开始通过并行处理方法看到性能改进。 java-8流提供了一种简单的并行计算方法。
你只需要稍微重构你的代码。
public class Palindrome {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
System.out.println(getMaxOccurrenceValue(scan.nextLine()));
}
}
/** Retrieve maximum occurrence value */
public static int getMaxOccurrenceValue(String pal) {
return getAllPalindromes(pal).entrySet().stream()
.map(entry -> entry.getKey().length() * entry.getValue())
.mapToInt(Integer::intValue)
.max().orElse(0);
}
/** Retrieve all unique palindromes for given str with occurrence amount of each palindrome */
private static Map<String, Integer> getAllPalindromes(String str) {
Map<String, Integer> map = new TreeMap<>();
for (int i = 0; i < str.length(); i++) {
for (int j = i + 1; j < str.length(); j++) {
String sub = str.substring(i, j);
if (isPalindrome(sub))
map.compute(sub, (key, count) -> Optional.ofNullable(count).orElse(0) + 1);
}
}
return map;
}
/** Check is given str palindrome or not */
private static boolean isPalindrome(String str) {
for (int i = 0, j = str.length() - 1; i < j; i++, j--)
if (str.charAt(i) != str.charAt(j))
return false;
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.