# 最短的普通超弦

26

• `S('LOREM', 'DOLOR', 'SED', 'DO', 'MAGNA', 'AD', 'DOLORE')='SEDOLOREMAGNAD'`
• `S('ABCDE', 'BCD', 'C')='ABCDE'`

3
+1好问题。我建议您提供一些其他预期结果的示例，以便人们可以轻松判断提交的内容是否能够处理各种情况。
DavidC

flornquake

8

# Python 2中，170 153/157/159

``````from itertools import*
print min((reduce(lambda s,w:(w+s[max(i*(s[:i]==w[-i:])for i in range(99)):],s)[w in s],p)
for p in permutations(input())),key=len)
``````

``````>>> "AD", "DO", "DOLOR", "DOLORE", "LOREM", "MAGNA", "SED", "ORE",  "R"

>>> 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', 'abcdefghijklmnopqrstuvw
... xyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu
... vwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'ZOOM', 'aZ', 'Za', 'ZA'
aZABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZOOM
``````

5

# 数学337 418 372

``````v=Length;
p[t_]:=Subsets[t,{2}];
f[w_]:=Module[{c,x,s=Flatten,r={{a___,Longest[y__]},{y__,b___}}:>{{a,y},{y,b},{y},{a,y,b}}},
c=p@w;
x=SortBy[Cases[s[{#/.r,(Reverse@#)/.r}&/@c,1],{_,_,_,_}],v[#[[3]]]&][[-1]];
Append[Complement[w,{x[[1]],x[[2]]}],x[[4]]]]

g[r_]:=With[{h=Complement[r,Cases[Join[p@r,p@Reverse@r],y_/;!StringFreeQ@@y:>y[[2]]]]},
FixedPoint[f,Characters/@h,v@h-1]<>""]
``````

``````r={{a___,Longest[y__]},{y__,b___}}:> {{a,y},{y,b},{y},{a,y,b}}},
``````

`Reverse`稍后使用以确保两个订单都经过测试。那些共享可链接字母的对将保持不变并被忽略。

``````h=Complement[r,Cases[Join[p@r,p@Reverse@r],x_/;!StringFreeQ@@x:> x[[2]]]]
``````

`````` {{"D", "O", "L", "O", "R", "E"}, {"L", "O", "R", "E", "M"}} /. r
``````

{{“ D”，“ O”，“ L”，“ O”，“ R”，“ E”}，{“ L”，“ O”，“ R”，“ E”，“ M”}，{ “ L”，“ O”，“ R”，“ E”}}，{“ D”，“ O”，“ L”，“ O”，“ R”，“ E”，“ M”}}

``````g[{"LOREM", "ORE", "R"}]

AbsoluteTiming[g[{"AD", "DO", "DOLOR", "DOLORE", "LOREM", "MAGNA", "SED", "ORE",  "R"}]]
``````

“ LOREM”

flornquake

（即，产生的输出正确`"LOREM"`吗？）
flornquake

@flornquake。不错的收获。我在当前版本中解决了它。我希望我没有错过任何其他情况。谢谢。
DavidC

DavidC

3

### GolfScript，66个字符

``````{.,1>{.`{[1\$]-s:h;.,),\`{:g<`{\+.g?0<{;}*}+h%~}+/}+%.&}*}:s~{,}\$0=
``````

``````['LOREM' 'DOLOR' 'SED' 'DO' 'MAGNA' 'AD' 'DOLORE']
{.,1>{.`{[1\$]-s:h;.,),\`{:g<`{\+.g?0<{;}*}+h%~}+/}+%.&}*}:s~{,}\$0=

['AB' 'BC' 'CA' 'BCD' 'CDE']
{.,1>{.`{[1\$]-s:h;.,),\`{:g<`{\+.g?0<{;}*}+h%~}+/}+%.&}*}:s~{,}\$0=
# => CABCDE
``````

2

# python 2，203 187 200

``````from itertools import permutations as p
def n(c,s=''):
for x in c:s+=x[next((i+1 for i,l in [(j,x[:j+1])for j in range(len(x))][::-1]if s.endswith(l)),0):]
return s
print min(map(n,p(input())),key=len)
``````

``````print min((reduce(lambda a,x:a+x[next((i+1 for i,l in [(j,x[:j+1])for j in range(len(x))][::-1]if a.endswith(l)),0):],P,'')for P in __import__('itertools').permutations(input())),key=len)
``````

``````print min((reduce(lambda a,x:a+(x[next((i+1 for i,l in [(j,x[:j+1])for j in range(len(x))][::-1]if a.endswith(l)),0):],'')[x in a],P,'')for P in __import__('itertools').permutations(input())),key=len)
``````

``````from itertools import permutations

def solve(*strings):
"""
Given a list of strings, return the shortest string that contains them all.
"""
return min((simplify(p) for p in permutations(strings)), key=len)

def prefixes(s):
"""
Return a list of all the prefixes of the given string (including itself),
in ascending order (from shortest to longest).
"""
return [s[:i+1] for i in range(len(s))]
return [(i,s[:i+1]) for i in range(len(s))][::-1]

def simplify(strings):
"""
Given a list of strings, concatenate them wile removing overlaps between
successive elements.
"""
ret = ''
for s in strings:
if s in ret:
break
for i, prefix in reversed(list(enumerate(prefixes(s)))):
if ret.endswith(prefix):
ret += s[i+1:]
break
else:
ret += s
return ret

print solve('LOREM', 'DOLOR', 'SED', 'DO', 'MAGNA', 'AD', 'DOLORE')
``````

flornquake

@flornquake好收获。我设法修复了它，但它增加了13个字符。
Baptiste M.

1

## Python，144个字符

``````S=lambda A,s:min(S(A-set([a]),s+a[i:])for a in A for i in range(len(a)+1)if i==0 or s[-i:]==a[:i])if A else(len(s),s)
T=lambda L:S(set(L),'')[1]
``````

`S`接受一组`A`仍需要放置的单词和一个`s`包含到目前为止放置的单词的字符串。我们`a`从中选择一个剩余的单词，`A`并将其从重叠`0``len(a)`字符的末尾`s`

flornquake

0

# 哈斯克尔（121）

``````import Data.List
a p []=[(length p,p)]
a p s=[r|w<-s,t<-tails w,isInfixOf w\$p++t,r<-a(p++t)(s\\[w])]
s=snd.minimum.a ""
``````