【发布时间】:2015-03-06 08:10:54
【问题描述】:
我正在寻找一种允许用户输入字符串并且程序将返回最长方形子字符串的算法(在 Java 中)。例如,如果用户输入“poofoofoopoo”,则程序返回“Longest Square Substring: foofoo”。如果有人能写出这样的算法,我将不胜感激!
我的第一个想法是修改 Manacher 算法,该算法返回最长的回文子串(在线性时间内)。
这是我为 Manacher 算法准备的 Java 代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class LongestPalindrome
{
// function to pre-process string
public String preProcess(String str)
{
int len = str.length();
if (len == 0)
{
return "^$";
}
String s = "^";
for (int i = 0; i < len; i++)
{
s += "#" + str.charAt(i);
}
s += "#$";
return s;
}
// function to get largest palindrome sub-string
public String getLongestPalindrome(String str)
{
// pre-process string
char[] s = preProcess(str).toCharArray();
int N = s.length;
int[] p = new int[N + 1];
int id = 0;
int mx = 0;
for (int i = 1; i < N - 1; i++)
{
p[i] = 0;
while (s[i + 1 + p[i]] == s[i - 1 - p[i]])
{
p[i]++;
}
if (i + p[i] > mx)
{
mx = i + p[i];
id = i;
}
}
// length of largest palindrome
int maxLen = 0;
// position of center of largest palindrome
int centerIndex = 0;
for (int i = 1; i < N - 1; i++)
{
if (p[i] > maxLen)
{
maxLen = p[i];
centerIndex = i;
}
}
// starting index of palindrome
int pos = (centerIndex - 1 - maxLen)/2;
return str.substring(pos , pos + maxLen);
}
// Main Function
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("LongestPalindrome Algorithm Test\n");
System.out.println("\nEnter String");
String text = br.readLine();
LongestPalindrome m = new LongestPalindrome();
String LongestPalindrome = m.getLongestPalindrome(text);
System.out.println("\nLongest Palindrome: "+ LongestPalindrome);
}
}
【问题讨论】:
标签: java string algorithm combinatorics words