Word Maker (APCS 2023-01 Intermediate)

Points 100 1.0s 256M

You are given a lowercase English string \(S\) of length \(K\). The word-making program rearranges its characters according to a permutation \(P_1,P_2,\ldots,P_K\) of the integers from \(1\) to \(K\).

In one operation, the character at position \(i\) of the old string is copied to position \(P_i\) of the new string. In other words,

\[\text{new string}[P_i]=\text{old string}[i] \qquad (1\le i\le K).\]

Since \(P\) is a permutation, every position in the new string receives exactly one character. After an operation, the new string becomes the old string for the next operation.

The following diagram applies the permutation \((4,1,3,2)\) to abac. The characters at old positions \(1,2,3,4\) move to new positions \(4,1,3,2\), respectively, producing bcaa.

The four characters of the old string abac move to their destination positions according to permutation 4 1 3 2, producing bcaa

The program performs \(Q\) operations in order, each with a new permutation. Given \(R\), report the first \(R\) positions separately. Output one line for each position \(j\): concatenate the characters at position \(j\) after operations \(1,2,\ldots,Q\), in that order.

Input

The first line contains three integers \(K,Q,R\).

The second line contains the lowercase string \(S\) of length \(K\).

Each of the next \(Q\) lines contains \(K\) integers \(P_1,P_2,\ldots,P_K\), the permutation used by that operation.

Constraints

  • \(1\le R\le K\le 20\)
  • \(1\le Q\le 20\)
  • \(S\) contains only lowercase English letters.
  • For every operation, \(P_1,P_2,\ldots,P_K\) is a permutation of the integers from \(1\) to \(K\).

Output

Output \(R\) lines.

Line \(j\) must contain a string of length \(Q\): the characters at position \(j\) after each operation, in chronological order.

Scoring

There are two subtasks:

Subtask Points Additional constraint
1 60 \(R=1\)
2 40 No additional constraints

Sample Input 1

5 4 1
abcde
2 1 3 5 4
5 1 2 4 3
4 1 2 3 5
3 1 4 5 2

Sample Output 1

bacd

Sample Explanation 1

Sample one transforms abcde into baced, acdeb, cdeab, and dbcea; their first characters are b, a, c, and d

The strings after the four operations are baced, acdeb, cdeab, and dbcea. Since \(R=1\), only the first character is recorded after each operation. These characters are b, a, c, and d, so the output is bacd.

Sample Input 2

4 3 4
abac
4 1 3 2
1 2 3 4
2 3 4 1

Sample Output 2

bba
ccb
aac
aaa

Sample Explanation 2

Sample two transforms abac into bcaa, bcaa, and abca; reading each position through time gives bba, ccb, aac, and aaa

The strings after the three operations are bcaa, bcaa, and abca. Reading each of the four positions through these three states gives the output lines bba, ccb, aac, and aaa.

Source

2023 January APCS Programming Practice, Problem 2: ZeroJudge j606, Word Maker. The presentation and sample explanations also refer to Yi-Zhe Wang's HackMD solution.

Problem page help

Keyboard shortcuts

Main features

  • Sample tests — Runs the sample cases bundled with the problem and auto-compares against the expected output.
  • Custom test — Run your code with your own stdin. Optionally tick the "Compare with expected (diff)" box to verify against expected output line-by-line.
  • Template — Paste the default code template you set on your profile page.
  • Collab — Edit this problem together with classmates in real time.
  • Auto-draft — Editor contents auto-save to your browser every 1.5 seconds (per account / problem / language).
  • Submit — Send your code to the judge for grading; returns AC / WA / TLE etc.

Limits

  • Source code: at most 65,536 characters
  • Custom test stdin and expected output: at most 1 MB each (≈1 million characters)
  • Custom test and sample test share the sandbox; about 1 request per 3 s per user (sample test: 1 per 1 s)
  • Custom test and sample test both have a 15 second wall-clock cap (the official judge still uses the problem time limit)
  • Interactive problems do not offer custom test (cannot simulate interaction with the judge).
  • Submitting has no rate limit, but rapid repeated submissions on the same problem are treated as score farming.