J'ai écrit un code qui calcule la superstringle la plus courte à partir d'un tableau de valeurs.
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);
}
}
Ce que je dois calculer, c'est le nombre minimum d'éléments du tableau qui peuvent être concaténés pour créer la plus courte des supercordes.
Par exemple, le code fonctionne actuellement comme suit.
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
La partie supplémentaire de la sortie que je dois calculer devrait ressembler à ceci
Solution = s[3]+s[0]+s[5]
Toute aide serait très appréciée !