最短的超级串

时间:2018-02-24 13:06:16

标签: java

我编写了一些代码,用于计算值数组中最短的超弦。

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]

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

正如我评论的那样,您的算法只能查找和逼近最短的超弦。一个正确的(但速度要慢得多)算法大致如下:

  • 初始化best_permutation = null和shortest_size =无限
  • 计算子串序列的所有排列(例如,使用Heaps算法)
  • 对于每个排列,&#34;挤压&#34;尽可能地相互邻居
  • 如果结果大小比shortest_size短,则为shortest_size和best_permutation分配新值

在此算法结束时,您将获得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,您可以从中轻松生成所需的输出。