Efficient word scramble algorithm

前端 未结 4 1678
情歌与酒
情歌与酒 2020-12-30 06:13

I\'m looking for an efficient algorithm for scrambling a set of letters into a permutation containing the maximum number of words.

For example, say I am given the li

相关标签:
4条回答
  • 2020-12-30 06:42

    Here's an idea, inspired by Markov Chains:

    1. Precompute the letter transition probabilities in your dictionary. Create a table with the probability that some letter X is followed by another letter Y, for all letter pairs, based on the words in the dictionary.
    2. Generate permutations by randomly choosing each next letter from the remaining pool of letters, based on the previous letter and the probability table, until all letters are used up. Run this many times.
    3. You can experiment by increasing the "memory" of your transition table - don't look only one letter back, but say 2 or 3. This increases the probability table, but gives you more chance of creating a valid word.
    0 讨论(0)
  • 2020-12-30 06:49

    It might be useful to check how others solved this: http://sourceforge.net/search/?type_of_search=soft&words=anagram

    On this page you can generate anagrams online. I've played around with it for a while and it's great fun. It doesn't explain in detail how it does its job, but the parameters give some insight. http://wordsmith.org/anagram/advanced.html

    0 讨论(0)
  • 2020-12-30 06:57

    You might try simulated annealing, which has been used successfully for complex optimization problems in a number of domains. Basically you do randomized hill-climbing while gradually reducing the randomness. Since you already have the Aho-Corasick scoring you've done most of the work already. All you need is a way to generate neighbor permutations; for that something simple like swapping a pair of letters should work fine.

    0 讨论(0)
  • 2020-12-30 07:02

    Have you thought about using a genetic algorithm? You have the beginnings of your fitness function already. You could experiment with the mutation and crossover (thanks Nathan) algorithms to see which do the best job.

    Another option would be for your algorithm to build the smallest possible word from the input set, and then add one letter at a time so that the new word is also is or contains a new word. Start with a few different starting words for each input set and see where it leads.

    Just a few idle thoughts.

    0 讨论(0)
提交回复
热议问题