在递归中查找Java String Array的无序序列

时间:2014-09-11 14:00:34

标签: java arrays string recursion permutation

我必须创建一个算法,该算法使用String数组显示所有可用的固定长度的无序序列。该方法需要递归,并且可能只需要一个整数。它还需要返回一个String数组。

假设我有“ab”和“ba”。 当我用方法给出int 2时,应该找到以下无序的固定长度序列:

abab
abba
baba
baab

我已经工作了好几个小时了,我觉得我为这件简单的事情努力工作。我有不同类型的代码,我几乎可以工作(abba显示两次而不是另一个序列)但我忘了将它返回到一个数组中,从而导致问题...... 我看到的代码看起来像这样,但是未完成但不起作用:

static String[] syllables = {"ab", "ba"};
static String[] syllableWord;

public static void main(String[] args) {
    int amountOfSillables = 2;
    syllableWord = String[(int)Math.pow(amountOfSillables, amountOfSillables)];
    String[] syllableWords = findSequences(amountOfSillables); // I may only use one parameter,
                                     // which is 2 but should work with any number

    for (int i = 0; i < syllableWords.length; i++) {
        System.out.println(syllableWords[i]);
    }
}

public static String[] findSequences(int n) {
    if (n == 0) {
        return syllableWord;
    }
    else {
        for (int i = 0; i < syllables.length; i++) {
            syllableWord += syllables[i]; // Doesn't work because syllableWord is an array.
                                          // When it's a String this would kinda work, but it 
                                          // needs to be an array.
            findSequences(n - 1);
            syllableWord = syllableWord.substring(2); // Also doesn't work when it's an array.
        }
    }
}

有人可以帮帮我吗?这让我发疯了......

3 个答案:

答案 0 :(得分:1)

这样的东西? (使用ArrayList比Array更聪明,因为你不需要管理数组大小,取决于你的需要)

public static List<String> perm(int n) {
    List<String> result = new ArrayList<String>();
    if (n == 1) {
        return Arrays.asList(syllables);
    }
    for (String s : syllables) {
        for (String prefix : perm(n - 1)) {
            result.add(s + prefix);
        }
    }
    return result;
}

答案 1 :(得分:0)

我并没有完全使用你的模板,我在递归调用中使用的不仅仅是一个整数。

我只使用一个数组,并且在递归调用之后几乎没有后计算,所以它应该非常有效。

public static String[] perm(String[] data) {
    int n = (int) Math.pow(data.length, data.length);
    String[] result = new String[n];
    algo(data, result, 0, n, new StringBuilder());
    return result;
}

private static void algo(String[] data, String[] result, int offset, int width, StringBuilder acc) {
    if(width == 1) {
        result[offset] = acc.toString();
        return;
    }
    width /= data.length;
    for(int i=0; i<data.length; i++) {
        acc.append(data[i]);
        algo(data, result, offset+i*width, width, acc);
        int s = acc.length();
        acc.delete(s-data[i].length(), s);
    }
}

答案 2 :(得分:0)

假设您只想显示该值,则以下内容应该有效:

public static void main(String args[])
{

    final String[] syllables = new String[] { "ab", "ba" };
    final List<String> path = new ArrayList<String>();
    recursiveDisplay(syllables, path, 0);

}

private static void recursiveDisplay(final String[] syllables, List<String> path, final int index)
{

    if (index < syllables.length - 1)
        for (int i = 0; i < syllables.length; i++)
        {
            path.add(syllables[i]);//add current token
            recursiveDisplay(syllables, path, index + 1);//process updated path
            path.remove(path.size() - 1);//remove processed token
        }
    else
    {
        // we have reached the end of the tree
        // let's display all possible combination of this path

        StringBuilder buidler = new StringBuilder();
        for (final String token : path)
            buidler.append(token);

        for (final String token : syllables)
            System.out.println(buidler + token);
    }

}