如何创建一个递归字谜工具,它打印的信件,其中包括prefixes一个字符串的每一个可能的组合? [英] How do I create a recursive anagram tool which prints every possible combination of a string of letters, including prefixes?

查看:146
本文介绍了如何创建一个递归字谜工具,它打印的信件,其中包括prefixes一个字符串的每一个可能的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个字谜方法来创建一个字的所有可能的组合,但我希望能够检查每一个字母的组合。

例如:东部会产生其他变化,如认真,但我也很喜欢它产生的变化,如东,吃,吃了

我已经有一个工作的字典检查组合是否在字典中。

 公共无效printAnagrams(字符串preFIX,串字)
{
    如果(word.length()== 1){
        如果(words.contains(preFIX +字))
            的System.out.println(preFIX +字);
    }
    其他
    {
        的for(int i = 0; I< word.length();我++)
        {
            串电流= word.substring(I,I + 1);
            字符串之前= word.substring(0,I);
            后字符串= word.substring第(i + 1);
            printAnagrams(preFIX +目前,前+后);
        }
    }
}
 

解决方案

有一组字符 [可能是一个的char [] ]和猜测,这是下一个字母,将其追加到一个中间溶胶的StringBuilder,它保存了当前字符串。调用递归算法来找出解决方案的下一个字符。

有一个指数来说明哪些字符可以使用的所有字符的权利

在每次迭代 - 打印生成的字符串,即使你没有的最大长度

伪code:

  findPermutations(字符,我,SOL):
   打印溶胶
   如果(chars.length ==ⅰ):
       返回
   有效范围内的所有因素(J,chars.length):
       sol.append(字符[I + 1])
       掉期(字符,I,J)//我们不能使用字符[J]了。
       findPermutations(字符,J + 1,溶胶)
       掉期(字符,I,J)//清理环境
       sol.removeLast()
 

注:

  1. 在此解决方案假定字符是一组,因此,如果某个字符出现超过一次 - 你将不得不重复的,但它仍然可以工作[生成所有可能性]
  2. 有关复杂性的一个字:有可能性的指数数量,并且希望所有的人。所以,如果你试图调用此方法与更多然后20​​个字符[实际上任何算法来做到这一点],您预计完成生成所有可能性,约200年

I've made an anagram method to create all the possible combinations of a word, but I'd like to be able to check every letter combination.

For example: eastern would produce other variations such as earnest, but I'd also like it to produce variations such as east and eat and ate.

I already have a working dictionary checking whether the combinations are in the dictionary.

public void printAnagrams(String prefix, String word)
{
    if(word.length() == 1) {
        if (words.contains(prefix+word))
            System.out.println(prefix + word);
    }
    else    
    {
        for(int i = 0; i < word.length(); i++)
        {
            String current = word.substring(i, i + 1);
            String before = word.substring(0, i);
            String after = word.substring(i+1);
            printAnagrams(prefix + current, before + after);
        }
    }
}

解决方案

Have a set of chars [can be a char[]] and "guess" which is the next letter, and append it to an intermediate sol StringBuilder, which holds the current substring. Invoke the algorithm recursively to find out next chars of the solution.

Have an index i to indicate which chars can be used [all chars "right" to i].

At each iteration - print the resulting string, even if you did not the maximal length.

Pseudo code:

findPermutations(chars,i,sol):
   print sol
   if (chars.length == i):
       return
   for each j in range(j,chars.length):
       sol.append(chars[i+1])
       swap(chars,i,j) //we cannot use chars[j] anymore.
       findPermutations(chars,j+1,sol)
       swap(chars,i,j) //clean up environment
       sol.removeLast()

Notes:

  1. This solution assumes chars is a set, so if a certain char appears more then once - you are going to have duplicates, but it will still work [generate all possibilities]
  2. A word about complexity: There are exponential number of possibilities, and you want all of them. So, if you try to invoke this method [and actually any algorithm to do it] with more then 20 chars, you are expected to finish generating all possibilities in ~200 years

这篇关于如何创建一个递归字谜工具,它打印的信件,其中包括prefixes一个字符串的每一个可能的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆