回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<h2>魁德勒解算器</h2>
<p>我有一个纸牌游戏叫做Quiddler,我正试图写一个算法来求解,但是当我试图线性求解它时,它非常缓慢且效率低下。在</p>
<p><strong>游戏(一步一步):</strong></p>
<ol>
<li>每一个牌手都会得到3到10张牌,每张牌上有一个或两个字母。在</li>
<li>然后,玩家尝试从他们得到的牌中做出一个或多个单词,而不必有多余的牌。在</li>
</ol>
<p>当我尽我最大的努力去完成这些看似简单的任务时,即使是中等长度的手,也要花20多秒才能找到所有的答案。在</p>
<p>我用字典<a href="http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain" rel="nofollow">such as this one</a>作为我的词表。我线性地检查我手上的字母数量,并将其与列表中的单词进行比较,假设它们长度相等或更短。虽然这是可行的,但却需要太长时间。在</p>
我希望有人能帮助我,最好是在Perl,Python或C/C++。在</p>
<p><strong>示例手:</strong>
卡片=['i'、'd'、'o'、'n']</p>
<p><strong>答案(根据我的算法):</strong>
di编号,
打开,
进去吧,
身份证号码,
身份证开了,
在做,
在od中,
恐龙,
诺迪</p>
<h2>我的Python算法:</h2>
<pre><code>import timeit
from wordlist import *
#Quiddler Solver
print 'Dictionary loaded\n'
#Define our hand
cards=['i','d','o','n']
#Count the letters in a word
def cntLetters(word): #Surely there's a better way?
lettercnt={97:0,98:0,99:0,100:0,101:0,102:0,103:0,104:0,105:0,106:0,107:0,108:0,109:0,110:0,111:0,112:0,113:0,114:0,115:0,116:0,117:0,118:0,119:0,120:0,121:0,122:0}
for v in word:
lettercnt[ord(v)]+=1
return lettercnt
#Check the letters to make sure our hand has at least what the word has
def cmpList(list1,list2):
for k,v in list1.iteritems():
if list2[k]<=v:
pass
else:
return False
return True
#Check to make sure cards with more than one letter are together in the word.
def has(word):
for v in cards:
if len(v)>1:
if v in word:
pass
else:
return False
return True
def solve():
rawhand=''.join(cards).lower()
hand=cntLetters(rawhand)
handl=len(rawhand)
buff=[]
for v in dict: #Add all words that have at least the letters in our hand to buff
if len(v)<=handl and cmpList(hand,cntLetters(v)):
if has(v):
buff.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(v)
for v in range(0,int((len(buff)/2)+1)): #Find 2 words that can be used together to make a play
for n in buff:
if len(n)==(handl-len(buff[v])):
if hand==cntLetters(buff[v]+n):
print buff[v],n
for v in range(0,int((len(buff)/3)+1)): #This is a bit overkill since it finds so much stuff, need to tune it
for n in buff:
if (len(n))<=len(buff[v]):
for x in buff:
if len(x)==(handl-len(buff[v])-len(n)):
if hand==cntLetters(buff[v]+n+x):
print buff[v],n,x
for v in buff: #Print the single words that can be made
if len(v)==handl:
print v
t = timeit.Timer(stmt=solve)
print 'Search took %.2f seconds'%t.timeit(number=1)
</code></pre>
<p><em>我从wordlist导入一个名为dict的预编译单词列表。</em></p>
<p>我希望有人能帮我设计算法,因为它需要改进,谢谢。在</p>
<p>有人建议用破折号,但我不查单词。在这种情况下,我还是要循环单词来检查字母,除非我的思路有误?在</p>