Skip to content

Instantly share code, notes, and snippets.

@catupper
Created September 6, 2012 11:27
Show Gist options
  • Select an option

  • Save catupper/3655120 to your computer and use it in GitHub Desktop.

Select an option

Save catupper/3655120 to your computer and use it in GitHub Desktop.
cheat program of word hero
Dictfile="Dictionary_path"
f=open(Dictfile,"r")
p=[]
LEN=0
for line in f:
line=line.strip()
if len(line)<3:continue
p+=[line]
LEN+=1
p.sort()
def search(word,down=0):
up=LEN
while up-down>2:
mid=(down+up)/2
if p[mid]<word:
down=mid
else:
up=mid
res=(down+up)/2
while res>0 and p[res]>word:res-=1
while p[res]<word:res+=1
if p[res]==word:
return 1,res
elif word in p[res]:
return -1,res
else:return -1,-1
def solve(maps,alfa):
used=[False]*16
pres=[]
def dfs(a,word,down,index):
used[a]=True
word+=alfa[a]
p,u=search(word,down)
if u==-1:
used[a]=False
return
for x in maps[a]:
if not used[x]:
dfs(x,word,u,index+[x])
if p!=-1:
pres.append((word,index))
used[a]=False
for x in xrange(16):
dfs(x,"",0,[x])
pres.sort(reverse=True)
pres.sort(key=lambda x:len(x[0]),reverse=True)
p=1
now=pres[0][0]
while p < len(pres):
if pres[p][0]==now:
del pres[p]
else:
now=pres[p][0]
p+=1
return pres
forformap=[[]for x in xrange(16)]
for x in xrange(16):
l,r=x/4,x%4
for i in xrange(-1,2):
for j in xrange(-1,2):
if i==j==0:continue
if 0<=l+i<4 and 0<=r+j<4:
forformap[x].append((l+i)*4+r+j)
def main():
alfa=raw_input().split()
for x in solve(forformap,alfa):
print x
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment