查找具有给定单词的所有字符的所有单词 [英] Find all words that have all characters of a given word

查看:110
本文介绍了查找具有给定单词的所有字符的所有单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建文字游戏。我在一个文本文件中列出了英语词典中所有单词的列表。我在此列表中选择一个随机词。一旦有了随机词,就需要选择所有具有所选随机词中所有字符的词。

I am creating a word game. I have a list of all the words in the english dictionary in a text file. I choose a random word in this list. Once I have the random word then I need to choose all the words that have all the characters in the choosen random word.

我需要一种策略来做到这一点。另外,我应该将单词列表放置在文本文件还是数据库中。最好的策略是什么?

I need a strategy to do this. Also, shall i place the word list in a text file or a database. What is the best strategy to do this?

编辑

匹配示例:


  • The匹配他,他、,、 te NOT-> Thee,Tee

  • 如您在上面的示例中看到的, foil匹配油,如果不是->填充,傻瓜,掉油,

随机单词不能与具有更多字符的单词或具有比单个单词更多的单个字符的单词匹配

As you can see in the above examples the random word must not match words that have more characters or characters that have more of a single character then the random word

例如:


  • e必须与ee不匹配

  • el必须与eel不匹配

  • 很多不能与赃物匹配

推荐答案

对于使用拉丁字母的语言中的单词,您可以计算单词的26位只有在单词包含字母#i时,才将位#i设置为1。

For words in languages that use latin alphabet, you can calculate word's 26-bit "signature" by setting bit #i to one only when the word contains letter #i of the alphabet:

var signature = 0;
foreach (var c in word.ToUpperCase()) {
    signature |= (1<<(c-'A'));
}

然后可以将签名及其词和词的长度存储在数据库。单词匹配后,需要计算其签名,然后在数据库中查询所有与签名和目标单词长度匹配的单词。对于每个具有匹配长度和签名的候选单词,将其转换为大写字母,对其字母排序,然后将排序结果与排序目标进行比较。如果目标匹配,则将单词添加到答案列表中。

You can then store the signature along with its word and the word's length in the database. Once you get the word you need to match, calculate its signature, and query the database for all words that match the signature and the target words's length. For each candidate word with matching length and signature, convert the word to upper case, sort its letters, and compare the sorted result to sorted target. If the target matches, add the word to the list of answers.

这篇关于查找具有给定单词的所有字符的所有单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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