String Operations (APCS 2025-01 Intermediate)

Points 100 1.0s 256M

You are given an even-length string \(S\) containing only lowercase English letters. There are three types of operations:

  1. Operation 0: swap adjacent pairs

    Group adjacent characters from left to right and swap the two characters in every group. For example, grouping apcsntnu as (ap)(cs)(nt)(nu) gives (pa)(sc)(tn)(un), so the result is pasctnun.

  2. Operation 1: sort adjacent pairs

    Group adjacent characters from left to right and sort the two characters in every group according to \(a<b<\dots<z\). Characters from different groups are not sorted with one another. For example, grouping family as (fa)(mi)(ly) gives (af)(im)(ly), so the result is afimly.

  3. Operation 2: perfect shuffle

    Let the string length be \(n\). Split the string into two halves of equal length, then alternately take the \(i\)-th character from the first half and the \(i\)-th character from the second half.

    Equivalently, characters originally at indices

    \[0,1,\dots,\frac n2-1\]

    and

    \[\frac n2,\frac n2+1,\dots,n-1\]

    are rearranged in the order

    \[0,\frac n2,1,\frac n2+1,\dots,\frac n2-1,n-1.\]

    For example, splitting apcsntnu into apcs and ntnu and interleaving the halves gives anptcnsu.

You will be given \(k\) operations. Apply all of them in input order and output the final string.

Input

The first line contains the string \(S\).

The second line contains a positive integer \(k\), the number of operations.

Each of the next \(k\) lines contains an integer \(op\), which is \(0\), \(1\), or \(2\).

Constraints

  • \(2\le |S|\le100\)
  • \(|S|\) is even
  • \(S\) contains only lowercase English letters from a to z
  • \(1\le k\le100\)
  • \(op\in\{0,1,2\}\)

Output

Print the string after all operations have been applied.

Scoring

The time limit for each test case is \(1\) second; your score is the sum over the test cases you pass. The subtasks are:

Subtask Points Additional constraints
1 60 $
2 40 No additional constraints

Sample Input 1

apcsntnu
1
2

Sample Output 1

anptcnsu

Sample Input 2

facebook
4
2
0
2
1

Sample Output 2

bocfkoae

Sample Explanation 2

The four operations transform facebook as follows:

  1. Operation 2: facebook becomes fbaocoek.
  2. Operation 0: fbaocoek becomes bfoaocke.
  3. Operation 2: bfoaocke becomes bofcokae.
  4. Operation 1: bofcokae becomes bocfkoae.

Source

Adapted from Problem 2, "String Operations," of the January 2025 APCS programming test (ZeroJudge q182). The statement preparation also referred to Yizhe Wang's HackMD solution note.

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.