我正在尝试实现Longest Common Substring的修改版本,我不想拆分这些单词。
例如:
String s1 = "hi there how are you";
String s2 = "hi there how ar";
所以输出应该是:
o / p:hi there how
。
我首先计算最长的公共子串,然后过滤任何被拆分的单词,以及相同的代码:
private static void longestCommonSubstring(String S1, String S2) {
int Start = 0;
int Max = 0;
for (int i = 0; i < S1.length(); i++) {
for (int j = 0; j < S2.length(); j++) {
int x = 0;
while (S1.charAt(i + x) == S2.charAt(j + x)) {
x++;
if (((i + x) >= S1.length()) || ((j + x) >= S2.length()))
break;
}
if (x > Max) {
Max = x;
Start = i;
}
}
}
System.out.println("S1 " + S1 + " S2 " + S2 + " " + Start + " " + Max);
System.out.println("ans " + S1.substring(Start, (Start+Max)));
if(Start != 0){
if((S1.charAt(Start-1) == ' ')){
if(Start+Max != S1.length()){
if((S1.charAt(Start+Max) == ' ')){
System.out.println(S1.substring(Start, (Start + Max)));
}
}
}
else if((S1.charAt(Start+Max) == ' ')){
int index = S1.indexOf(' ', Start);
System.out.println(S1.substring(index+1, (Start + Max)));
}
else{
int index = S1.indexOf(' ', Start);
int lastIndex = S1.lastIndexOf(' ', (Start+Max));
if(index+1 != lastIndex+1){
System.out.println(S1.substring(index+1, lastIndex));
}
else{
System.out.println(" ");
}
}
}
else if(Start == 0 && Start+Max != S1.length() && (S1.charAt(Start+Max) == ' ')){
System.out.println(S1.substring(Start, (Start + Max)));
}
else if(Start+Max != S1.length()){
int index = S1.lastIndexOf(' ', (Start+Max));
System.out.println(S1.substring(Start, index));
}
else{
System.out.println(S1.substring(Start, (Start + Max)));
}
}
但是对于像以下这样的案例来说却失败了:
String s1 = "hi there how are you";
String s2 = "i ere ho ar you";
输出应该是“你”但是空白, 因为最长的公共子串是“ere ho”。
感谢您的帮助。
答案 0 :(得分:2)
基于karthik,以及在bna的评论之后,任何基于字符的答案都存在缺陷:
public static ArrayList<String> stringToTokens(String s) {
ArrayList<String> al = new ArrayList<String>();
StringBuilder word = new StringBuilder();
boolean inWord = !s.isEmpty() && Character.isLetter(s.charAt(0));
for (int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if (Character.isLetter(c)) {
word.append(c);
} else if (inWord) {
if (word.length() > 0) {
al.add(word.toString());
word.setLength(0);
}
al.add(""+c);
} else {
al.add(""+c);
}
}
if (word.length() > 0) {
al.add(word.toString());
}
return al;
}
public static String longestCommonWithWords(String sa, String sb) {
ArrayList<String> a = stringToTokens(sa);
ArrayList<String> b = stringToTokens(sb);
int m[][] = new int[a.size()][b.size()];
int bestLength = 0;
HashSet<Integer> bestSet = new HashSet<Integer>();
for (int i=0; i<a.size(); i++) {
for (int j=0; j<b.size(); j++) {
if (a.get(i).equals(b.get(j))) {
m[i][j] = (i==0 || j==0) ? 1 : m[i-1][j-1]+1;
if (m[i][j] > bestLength) {
bestLength = m[i][j];
bestSet.clear();
bestSet.add(i);
} else if (m[i][j] == bestLength) {
bestSet.add(i);
}
} else {
m[i][j] = 0;
}
}
}
// return first result (or empty string if none)
StringBuilder result = new StringBuilder();
if (bestLength > 0) {
int end = bestSet.iterator().next();
int start = end - bestLength;
for (int i=start; i<end; i++) {
result.append(a.get(i+1));
}
}
return result.toString();
}
标记化很简单(StringTokenizer
也可以使用,但是这个版本可以更好地处理奇怪的字符)。 LCS算法直接来自https://en.wikipedia.org/wiki/Longest_common_substring_problem#Pseudocode
答案 1 :(得分:0)
您要做的是将文本拆分为单词,然后比较完整的单词,而不是单个字母。
答案 2 :(得分:0)
这有助于找到长度,我认为找到实际的字符串也不是那么困难。
使用您的输入创建单词数组
String s1 = "hi there how are you" => X[] ={"hi","there","how","are","you"}
String s1 = "hi there how are you" => Y[] ={"hi","there","how","ar"}
现在在阵列上找到LCS
LCS(X, Y, m, n) = LCS(X, Y, m-1, n-1) + 1 if X[m-1].equals(Y[n-1])
0 Otherwise (if !X[m-1].equals(Y[n-1]))