我编写了一些代码,用于计算值数组中最短的超弦。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class ShortestCommonSuperstringAlgorithm {
private void createSuperString(Set<String> subStrings) {
int totalStrings = subStrings.size();
String[] match = new String[totalStrings];
int i = 0;
for(String superString : subStrings) {
Set<String> temp = new HashSet<String>(subStrings);
String maxSuperString = superString;
while(temp.size() > 1) {
String subString = "";
String nextMaxSuperString = maxSuperString;
for(String nextString : temp) {
if(!nextString.equals(nextMaxSuperString)) {
String superTemp = getSuperString(maxSuperString, nextString);
if (nextMaxSuperString.equals(maxSuperString) || nextMaxSuperString.length() > superTemp.length()) {
nextMaxSuperString = superTemp;
subString = nextString;
}
}
}
temp.remove(maxSuperString);
temp.remove(subString);
maxSuperString = nextMaxSuperString;
temp.add(maxSuperString);
}
match[i] = maxSuperString;
//System.out.println(match[i]);
i++;
}
String bestAns = match[0];
for(i = 1; i < match.length; i++) {
if(bestAns.length() > match[i].length()) {
bestAns = match[i];
}
}
System.out.println("Shortest Common Super String => " + bestAns);
System.out.println("With a Length of => " + bestAns.length());
}
private String getSuperString(String superString, String someString) {
String result = superString;
int endIndex = someString.length() - 1;
while(endIndex > 0 && !superString.endsWith(someString.substring(0, endIndex))) {
endIndex--;
}
if(endIndex > 0) {
result += someString.substring(endIndex);
}
else {
result += someString;
}
return result;
}
public static void main(String arg[]) {
Set<String> fragments = new HashSet<String>();
ShortestCommonSuperstringAlgorithm superStringCreator = new ShortestCommonSuperstringAlgorithm();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = "";
int noOfFragments = 0; // noOfFragments = m
// read input string, no. of fragments and their length
try{
System.out.println("Enter the no of Fragments : ");
noOfFragments = Integer.parseInt(br.readLine());
int size = 1;
do{
System.out.println(size + ". Fragment String : ");
input = br.readLine();
fragments.add(input);
size++;
}while(size<=noOfFragments);
}catch(Exception ex){
System.out.println("Please give correct Inputs.");
ex.printStackTrace();
return;
}
// find the shortest superstring
superStringCreator.createSuperString(fragments);
}
}
我需要计算的是可以连接以创建最短超线程的数组元素的最小数量。
例如,代码目前的工作原理如下。
Input array: s[0] = ada
s[1] = dab
s[2] = dba
s[3] = adb
s[4] = bad
s[5] = bda
Shortest Common Super String => adbadabda
我需要计算的额外输出应该如下所示
Solution = s[3]+s[0]+s[5]
非常感谢任何帮助!
答案 0 :(得分:1)
正如我评论的那样,您的算法只能查找和逼近最短的超弦。一个正确的(但速度要慢得多)算法大致如下:
在此算法结束时,您将获得best_permutation以输出要打印的内容。
对于近似算法,最简单的方法是创建一个自定义类CombinedString来记住它有哪些子串&#34;吞下&#34;。
像
class CombinedString {
final String combinedValue;
final String[] substrings;
final int[] substringPositions;
public CombinedString(String initial) {
combinedValue = initial;
substrings = {initial};
substringPositions = [0]; // first string inserted at position 0
}
public CombinedString(CombinedString left, CombinedString right) {
// ...
}
// ...
}
然后您的方法签名会改变,如
private CombinedString getSuperString(CombinedString superString, CombinedString someString)
您的最终结果将是一个CombinedString,您可以从中轻松生成所需的输出。