BlockSorting
BlockSortingãåèã«è©¦ãã®å®è£ ããã¦ã¿ãã
# -*- coding: cp932 -*- # Blocksorting # å¤æ data = "abracadabra" mat = [ data[i:] + data[:i] for i in range(len(data))] for line in mat: print line print mat.sort() for line in mat: print line print print "result:" start = mat.index(data) print start tails = [line[-1] for line in mat] print tails # 復å print "restore:" heads = list(sorted(tails)) print heads cur = start i = 0 while True: tails[cur] = None c = heads[cur] print c, i += 1 if i == len(tails): break cur = tails.index(c)
ãã¡ãããã®ç´ æ´ãªå®è£ ã«ã§ãããã¼ã¿ãé£ããã¦ã¯ãããªãã
ã§ãå®è¡çµæã#以éã¯ã³ã¡ã³ã
abracadabra # ãããå ãã¼ã¿ bracadabraa # 1åãã¤ãããã¦ãã racadabraab acadabraabr cadabraabra adabraabrac dabraabraca abraabracad braabracada raabracadab aabracadabr aabracadabr # ä¸ã®è¡¨ãã½ã¼ããã abraabracad abracadabra acadabraabr adabraabrac braabracada bracadabraa cadabraabra dabraabraca raabracadab racadabraab result: 2 # å ãã¼ã¿ã®ãã£ãä½ç½® ['r', 'd', 'a', 'r', 'c', 'a', 'a', 'a', 'a', 'b', 'b'] # ä¸ã®è¡¨ã®æ«å°¾ãã¨ã£ããªã¹ãtails # ãã®äºã¤ãã復å ããã restore: ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'c', 'd', 'r', 'r'] # tailsãã½ã¼ããããã®ãã¤ã¾ã2çªç®ã®è¡¨ã®é ã®ãªã¹ã a b r a b r a c a d a # 復å ããã
ããã¼ã¼ã復å
ãããï¼
1æåãããã®è¡¨ãä½ãã¨ããã¯å®éã«ã¯å
ã®æååã®é©å½ãªã¨ãããèªãã ãã§ãããã楽ã ãã©ãæååã®é·ãåã®è¦ç´ æ°ã®ã½ã¼ããããã®ãããã©ããªãã
-
-
-
-
- -
-
-
-
ãããã°ãããã
ãã°ãçºçãããæå°éã®å ¥åãä½ã£ã¦ã¿ãã"aabbaa"ã§ãã°ããã¨ãå¤æã
print start print tails print heads while True: tails[cur] = None c = heads[cur] result.append(c) i += 1 if i == len(tails): break print c, tails cur = tails.index(c)
print start print tails print heads while True: tails[cur] = None c = heads[cur] result.append(c) i += 1 if i == len(tails): break print c, tails cur = tails.index(c)
ã»ãã»ããé ããæ¢ãã¡ããã¡ãªãã ãªã
- cur = tails.index(c) + try: + cur = tails.index(c, cur) + except: + cur = tails.index(c)
ãããããã£ã±ããã¡ã ã
aaaabb aaabba aabbaa abbaaa baaaab bbaaaa 2 ['b', 'a', 'a', 'a', 'b', 'a'] ['a', 'a', 'a', 'a', 'b', 'b'] a ['b', 'a', None, 'a', 'b', 'a'] a ['b', 'a', None, None, 'b', 'a'] b ['b', 'a', None, None, 'b', None] a [None, 'a', None, None, 'b', None] a [None, None, None, None, 'b', None] Traceback (most recent call last): File "c:/blocksorting.py", line 39, in <module> cur = tails.index(c) ValueError: list.index(x): x not in list
ãã¼ããaabã¾ã§æ¥ãã¨ãã«ãå ã«å é ã«ããbâaãé¸ã°ãã¦ãã¾ããªâ¦ã
-
-
-
-
- -
-
-
-
ãããªãã¨ãã¦ããå ´åãããªãã£ãããã
-
-
-
-
- -
-
-
-
ããè¦ããæåã®ãã¡ããã¨å¾©å
ããã¦ãªããã
ç²ãã¦ããã®ãï¼