字微调拼图


10

这是一个难题。

您的程序应在标准输入中接受两个单词。
第一个词是起始词。第二个单词是结束单词。

从开始的单词开始,您必须到达结束的单词,一次只能更改/添加/删除一个字母。每次修改后,它必须形成一个新的有效词。添加的字母被添加到开头或结尾。您可以从任何位置删除字母(但单词长度不能超过三个字母)。注意:您不能重新排列字母以形成单词。

程序的输出是从起始单词到结束单词的单词序列。

例:

Input:
    Post Shot

Output:
    Post
    cost
    coat
    goat
    got
    hot
    shot

优胜者:

  • 该程序必须在合理的时间内(少于10秒)运行。
  • 可以生成最短输出序列到奖赏词的程序。
    • Zink->硅
  • 如果一个以上的程序获得最短的序列,则最短的程序以char为单位(忽略空白)。
  • 如果我们仍然有多个计划提交日期/时间,则应使用。

笔记:


可能是“后期->锅->热->射击”更短。

@ S.Mark:然后您的算法击败了我,您就赢了。以上是可能解决方案的示例。较短的解决方案胜过较长的解决方案。
马丁·约克

故意地?对不起,我只是看错了。

2
我可以在Whitespace中解决程序大小为0的问题吗?

@Tim Nordenfur:我很想看看空白的实现。注意。在节目长度确定前,有两个规则可以决定获胜者。但是,如果您满足这些要求:-)
马丁·约克

Answers:


2

Python,288个字符

(不计算字典阅读线)

X=set(open('websters-dictionary').read().upper().split())

(S,E)=raw_input().upper().split()
G={S:0}
def A(w,x):
 if x not in G and x in X:G[x]=w
while E not in G:
 for w in G.copy():
  for i in range(len(w)):
   for c in"ABCDEFGHIJKLMNOPQRSTUVWXYZ":A(w,w[:i]+c+w[i+1:]);A(w,w[:i]+w[i+1:]);A(w,c+w);A(w,w+c)
s=''
while E:s=E+'\n'+s;E=G[E]
print s

接受挑战zinksilicon

ZINK
PINK
PANK
PANI
PANIC
PINIC
SINIC
SINICO
SILICO
SILICON

那本词典里有一些奇怪的单词。


我实际上没有检查内容。我只是试图找到每个人都可以使用的字典。
马丁·约克

尝试guester overturn(花费一些时间)或regatta gyrally(不返回);-)
Arnaud Le Blanc

是的,某些组合需要一段时间。最短的解决方案越长,时间就越长。最后一个没有解决方案-在这种情况下没有规范:)尽管很容易修改以进行处理(将句柄保存到G.copy()并在循环结束时将G与它进行比较) )。
基思·兰德尔

16

traceroute-10个字符

traceroute 

详情

post#traceroute shot

Type escape sequence to abort.
Tracing the route to shot (1.1.4.2)

  1 pot (1.1.1.2) 40 msec 68 msec 24 msec
  2 hot (1.1.3.2) 16 msec 32 msec 24 msec
  3 shot (1.1.4.2) 52 msec *  92 msec

路由器已预先配置为启用OSPF,并以这种方式安排。

在此处输入图片说明

是的,我需要233614路由器才能完全支持所有单词。:-)


非常聪明,但是您未通过10秒规则。配置所有路由器的时间将超过10秒。:-)出发路线是什么:Zink-> Silicon
Martin York

@Martin,请忽略配置时间,就像构建字典,呵呵,Zink的路由一样-> Silicon zink->pink->pank->pani->panic->pinic->sinic->sinico->silico->silicon我真的在尝试使用Dijkstra算法(在OSPF中使用),并且它可以找到1s左右的路径,我会我打完高尔夫球之后,再将其张贴在单独的帖子中。

3

PHP - 886 689 644 612

字典加载:

<?php foreach(file('websters-dictionary') as $line) {
    $word = strtolower(trim($line));
    if (strlen($word) < 3) continue;
    $c[$word] = 1;
}

实际代码(两者都同时使用):

list($d,$e)=explode(' ',strtolower(trim(`cat`)));$f=range(@a,@z);function w($a,&$g){global$c;if(isset($c[$a]))$g[]=$a;}$h[$d]=$b=@levenshtein;$i=new SplPriorityQueue;$i->insert($d,0);$j[$d]=0;$k[$d]=$b($d,$e);while($h){if(isset($c[$l=$i->extract()])){unset($h[$l],$c[$l]);if($l==$e){for(;$m=@$n[$o[]=$l];$l=$m);die(implode("\n",array_reverse($o)));}for($p=strlen($l),$g=array();$p--;){w(substr_replace($q=$l,"",$p,1),$g);foreach($f as$r){$q[$p]=$r;w($q,$g);w($r.$l,$g);w($l.$r,$g);}}foreach($g as$m){$s=$j[$l]+1;if(!isset($h[$m])||$s<$j[$m]){$n[$m]=$l;$i->insert($m,-(($k[$m]=$b($m,$e))+$j[$m]=$s));}$h[$m]=1;}}}

用法:

php puzzle.php <<< 'Zink Silicon'
# or
echo 'Zink Silicon'|php puzzle.php

结果:

zink
pink
pank
pani
panic
pinic
sinic
sinico
silico
silicon
(0.23s)

“ Zink Silicon”的运行时间应少于0.5秒,大多数情况下,运行时间应少于1秒(当不存在解决方案时,有时会更长,但仍会返回)。

这使用带有levenshtein距离A *算法来估计距离的下限。

一些有趣的测试:

  • vas arm-> vas bas bar barm arm(单词长于开始和结束)
  • oxy pom -> oxy poxy poy pom
  • regatta gyrally ->(无,但脚本正确终止)
  • aal presolution -> + 8个字符
  • lenticulated aal -> -9个字符
  • acarology lowness -> 46跳
  • caniniform lowness -> 51跳
  • cauliform lowness -> 52跳
  • overfoul lowness -> 54跳
  • dance facia ->路径中的某些单词比开始/结束都多了4个字符

试试Zink Silicon
Martin York

现在应该可以了:-)
Arnaud Le Blanc

我会找到一台更大的机器:PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes)
马丁·约克

您只需达到128M memory_limit设置;-)尝试使用php -dmemory_limit=256M
Arnaud Le Blanc

had->hand这是无效的举动,您只能在开头或结尾添加字母。同样适用于vest->verst:-)
Arnaud Le Blanc

3

蟒蛇

由于我无法将dijkstra代码压缩到数百个字节,因此这是我的非高尔夫版本。

import sys, heapq, time

# dijkstra algorithm from 
# http://code.activestate.com/recipes/119466-dijkstras-algorithm-for-shortest-paths/
def dijkstra(G, start, end):
   def flatten(L):
      while len(L) > 0:
         yield L[0]
         L = L[1]

   q = [(0, start, ())]
   visited = set()
   while True:
      (cost, v1, path) = heapq.heappop(q)
      if v1 not in visited:
         visited.add(v1)
         if v1 == end:
            return list(flatten(path))[::-1] + [v1]
         path = (v1, path)
         for (v2, cost2) in G[v1].iteritems():
            if v2 not in visited:
               heapq.heappush(q, (cost + cost2, v2, path))

nodes = tuple(sys.argv[1:])

print "Generating connections,",
current_time = time.time()

words = set(x for x in open("websters-dictionary", "rb").read().lower().split() if 3 <= len(x) <= max(5, *[len(l)+1 for l in nodes]))

print len(words), "nodes found"

def error():
    sys.exit("Unreachable Route between '%s' and '%s'" % nodes)

if not all(node in words for node in nodes):
    error()

# following codes are modified version of
# http://norvig.com/spell-correct.html
alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits(word):
   splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes = [a + b[1:] for a, b in splits if b]
   replaces = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   prepends = [c+word for c in alphabet]
   appends = [word+c for c in alphabet]
   return words & set(deletes + replaces + prepends + appends)

# Generate connections between nodes to pass to dijkstra algorithm
G = dict((x, dict((y, 1) for y in edits(x))) for x in words)

print "All connections generated, %0.2fs taken" % (time.time() - current_time)
current_time = time.time()

try:
    route = dijkstra(G, *nodes)
    print '\n'.join(route)
    print "%d hops, %0.2fs taken to search shortest path between '%s' & '%s'" % (len(route), time.time() - current_time, nodes[0], nodes[1])
except IndexError:
    error()

测验

$ python codegolf-693.py post shot
Generating connections, 15930 nodes found
All connections generated, 2.09s taken
post
host
hot
shot
4 hops, 0.04s taken to search shortest path between 'post' & 'shot'

$ python codegolf-693.py zink silicon
Generating connections, 86565 nodes found
All connections generated, 13.91s taken
zink
pink
pank
pani
panic
pinic
sinic
sinico
silico
silicon
10 hops, 0.75s taken to search shortest path between 'zink' & 'silicon'

添加了user300的测试

$ python codegolf-693.py vas arm
Generating connections, 15930 nodes found
All connections generated, 2.06s taken
vas
bas
bam
aam
arm
5 hops, 0.07s taken to search shortest path between 'vas' & 'arm'

$ python codegolf-693.py oxy pom
Generating connections, 15930 nodes found
All connections generated, 2.05s taken
oxy
poxy
pox
pom
4 hops, 0.01s taken to search shortest path between 'oxy' & 'pom'

$ python codegolf-693.py regatta gyrally
Generating connections, 86565 nodes found
All connections generated, 13.95s taken
Unreachable Route between 'regatta' and 'gyrally'

多一点

$ python codegolf-693.py gap shrend
Generating connections, 56783 nodes found
All connections generated, 8.16s taken
gap
rap
crap
craw
crew
screw
shrew
shrewd
shrend
9 hops, 0.67s taken to search shortest path between 'gap' & 'shrend'

$ python codegolf-693.py guester overturn
Generating connections, 118828 nodes found
All connections generated, 19.63s taken
guester
guesten
gesten
geste
gest
gast
east
ease
erse
verse
verset
overset
oversee
overseed
oversend
oversand
overhand
overhard
overcard
overcare
overtare
overture
overturn
23 hops, 0.82s taken to search shortest path between 'guester' & 'overturn'

3 <= len(x) <= max(map(len, [nodea, nodeb]))是否可以保证路径经过的单词永远不会超过开始和结束单词?
Arnaud Le Blanc

尝试oxy pom; 最短的路径是oxy->poxy->poy->pom。似乎您也可以在任何地方允许排列和插入,这是不允许的:-)
Arnaud Le Blanc

@ user300,固定的置换和插入部分,我复制粘贴了太多,谢谢;-)我将初始限制设置为5个字符,并允许再增加+1个字符的开始和结束字,让我知道是否仍然存在。谢谢。
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.