APCS實作解題: 造字程式(112年1月)

題目:https://zerojudge.tw/ShowProblem?problemid=j606

題目說明:

  • 有一個長度K 的初始字串S ,每個字元都是小寫的英文字母。
  • 接下來有Q次修改,每次修改都是一次字串重新排序,會給排序規則 P = [P1, P2…, PK]

解題概念:

  • 看清楚題目後直接解即可
  • 第i個字串會搬到Pi字元

Python 解:

import sys
all_inputs = sys.stdin.readlines()

kqr = all_inputs[0].split()
K, Q, R = int(kqr[0]), int(kqr[1]), int(kqr[2])

S = all_inputs[1]

A = []

for q in range(Q):
  arr = [ int(all_inputs[q+2].split()[i])  for i in range(K) ]

  new_S = [0]*K
  for i in range(K):
    new_S[arr[i]-1] = S[i]

  S = new_S
  A.append(S)



for r in range(R):
  output = ''
  for q in range(Q):
    output += A[q][r]

  print(output)