# 任意长度三进制Squarefree词

9

4

## GolfScript（40 27个字符）

``````~1,{.{!}%+}2\$*1,/<{,65+}%n+
``````

6

## 蟒蛇，94

``````n=input()
x=[0]
exec"x+=[1-y for y in x];"*n
print''.join('ABC'[x[i+1]-x[i]]for i in range(n))
``````

``````n=input()
x=[0]
while x==x[:n]:x+=[1-y for y in x]
print''.join('ABC'[x[i+1]-x[i]]for i in range(n))
``````

1
`exec"x+=[1-y for y in x];"*n`节省6个字符，却以效率为代价-嘿，这是高尔夫！
gnibbler

4

## Python中，129 125 119

``````s='A'
n=input()
while len(s)<=n:s=''.join('ABCBCACABBCAABCCABBCACABABCBCACABBCAABC'[ord(t)%5::3]for t in s)
print s[:n]
``````

1

grc 2013年

`while s[:n]==s:`保存1个以上
gnibbler

3

## Python2-112个字符

``````s='A'
n=input()
exec"s=''.join('ABCBCACABBCAABCCABBCACABABCBCACABBCAABC'[ord(t)%5::3]for t in s);"*n
print s[:n]
``````

2

# 数学159 140 134

``````g@n_:=StringTake[NestWhile[#~StringReplace~{"A"-> "ABCBACBCABCBA","B"-> "BCACBACABCACB",
"C"->"CABACBABCABAC"}&,"ABC",StringLength[#]<n&],n]
``````

``````g[10]
g[53]
g[506]
AbsoluteTiming[g[10^6];]
``````

`f` 将测试字符串是否为无正方形。

``````f[s_]:=StringFreeQ[s, x__~~x__]
``````

``````f@Out[336]
f@Out[337]
f@Out[338]
f["ABCBACBCABCBABCACBACCABCACBCABACBABCABACBCACBACABCACBA"]
``````

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.