"""
466 名前:デフォルトの名無しさん[sage] 投稿日:2014/07/10(木) 02:35:06.94 ID:W+p4qrtw
お題
昇順の数列A[a0,a1,..,an]とAの要素amを一つ指定する
Aの要素からなる数列Bを与えて[am,am+1,..,an,a0,a1,..,am-1]の順番で整列させよう
例
入: A = [2,3,5,7,9], a = 2, B = [9,2,5]
出: [2,5,9]
入: A = [2,3,5,7,9], a = 9, B = [9,2,5]
出: [9,2,5]
入: A = [2,3,5,7,9], a = 5, B = [9,2,5]
出: [5,9,2]
入: A = [2,3,5,7,9], a = 3, B = [9,2,5,5,2]
出: [5,5,9,2,2]
"""
def f(A,a,B):
ia = A.index(a)
SA = A[ia:] + A[:ia]
SB = sorted(B, key=lambda b: SA.index(b))
print '-'*3
print "入: A = {}, a = {}, B = {}".format(A,a,B)
print "出: {} (A' = {})".format(SB,SA)
f(A = [2,3,5,7,9], a = 2, B = [9,2,5])
f(A = [2,3,5,7,9], a = 9, B = [9,2,5])
f(A = [2,3,5,7,9], a = 5, B = [9,2,5])
f(A = [2,3,5,7,9], a = 3, B = [9,2,5,5,2])
IiIiCjQ2NiDlkI3liY3vvJrjg4fjg5Xjgqnjg6vjg4jjga7lkI3nhKHjgZfjgZXjgpNbc2FnZV0g5oqV56i/5pel77yaMjAxNC8wNy8xMCjmnKgpIDAyOjM1OjA2Ljk0IElEOlcrcDRxcnR3CuOBiumhjArmmIfpoIbjga7mlbDliJdBW2EwLGExLC4uLGFuXeOBqEHjga7opoHntKBhbeOCkuS4gOOBpOaMh+WumuOBmeOCiwpB44Gu6KaB57Sg44GL44KJ44Gq44KL5pWw5YiXQuOCkuS4juOBiOOBplthbSxhbSsxLC4uLGFuLGEwLGExLC4uLGFtLTFd44Gu6aCG55Wq44Gn5pW05YiX44GV44Gb44KI44GGCuS+iwrlhaU6IEEgPSBbMiwzLDUsNyw5XSwgYSA9IDIsIEIgPSBbOSwyLDVdCuWHujogWzIsNSw5XQrlhaU6IEEgPSBbMiwzLDUsNyw5XSwgYSA9IDksIEIgPSBbOSwyLDVdCuWHujogWzksMiw1XQrlhaU6IEEgPSBbMiwzLDUsNyw5XSwgYSA9IDUsIEIgPSBbOSwyLDVdCuWHujogWzUsOSwyXQrlhaU6IEEgPSBbMiwzLDUsNyw5XSwgYSA9IDMsIEIgPSBbOSwyLDUsNSwyXQrlh7o6IFs1LDUsOSwyLDJdCiIiIgoKZGVmIGYoQSxhLEIpOgogICAgaWEgPSBBLmluZGV4KGEpCiAgICBTQSA9IEFbaWE6XSArIEFbOmlhXQogICAgU0IgPSBzb3J0ZWQoQiwga2V5PWxhbWJkYSBiOiBTQS5pbmRleChiKSkKICAgIHByaW50ICctJyozCiAgICBwcmludCAi5YWlOiBBID0ge30sIGEgPSB7fSwgQiA9IHt9Ii5mb3JtYXQoQSxhLEIpCiAgICBwcmludCAi5Ye6OiB7fSAgKEEnID0ge30pIi5mb3JtYXQoU0IsU0EpCgpmKEEgPSBbMiwzLDUsNyw5XSwgYSA9IDIsIEIgPSBbOSwyLDVdKQpmKEEgPSBbMiwzLDUsNyw5XSwgYSA9IDksIEIgPSBbOSwyLDVdKQpmKEEgPSBbMiwzLDUsNyw5XSwgYSA9IDUsIEIgPSBbOSwyLDVdKQpmKEEgPSBbMiwzLDUsNyw5XSwgYSA9IDMsIEIgPSBbOSwyLDUsNSwyXSkK
---
入: A = [2, 3, 5, 7, 9], a = 2, B = [9, 2, 5]
出: [2, 5, 9] (A' = [2, 3, 5, 7, 9])
---
入: A = [2, 3, 5, 7, 9], a = 9, B = [9, 2, 5]
出: [9, 2, 5] (A' = [9, 2, 3, 5, 7])
---
入: A = [2, 3, 5, 7, 9], a = 5, B = [9, 2, 5]
出: [5, 9, 2] (A' = [5, 7, 9, 2, 3])
---
入: A = [2, 3, 5, 7, 9], a = 3, B = [9, 2, 5, 5, 2]
出: [5, 5, 9, 2, 2] (A' = [3, 5, 7, 9, 2])