- 注意如果上一次有反转,那么要将上一次最后一个数据的next改成本次反转后的开始的地址
测试结果为21分
def reverseList():
input = raw_input().split(' ')
input[1] = int(input[1])
input[2] = int(input[2])
inputList = []
global newstart
for i in range(input[1]):
inputList.append(raw_input().split(' '))
def indexFind(item, aimlist):
for i in range(len(aimlist)):
if aimlist[i][0] == item:
return i
def reverse(start, lastEnd, list):
next = start
address = start
for i in range(input[2]):
index = indexFind(next, list)
next = list[index][2]
list[index][2] = address
address = list[index][0]
index = indexFind(start, list)
list[index][2] = next
if lastEnd != 'NULL':
lastIndex = indexFind(lastEnd, inputList)
list[lastIndex][2] = address
lastEnd = list[index][0]
if start == input[0]:
global newstart
newstart = address
return next, lastEnd
start = input[0]
lastEnd = 'NULL'
count = input[1]
while count >= input[2]:
start, lastEnd = reverse(start, lastEnd, inputList)
count = count - input[2]
next = newstart
for i in range(len(inputList)):
index = indexFind(next, inputList)
print (' ').join(map(str, inputList[index]))
next = inputList[index][2]
if __name__ == '__main__':
reverseList()