String Operations (APCS 2025-01 Intermediate)
Points 100 1.0s 256MYou are given an even-length string \(S\) containing only lowercase English letters. There are three types of operations:
-
Operation 0: swap adjacent pairs
Group adjacent characters from left to right and swap the two characters in every group. For example, grouping
apcsntnuas(ap)(cs)(nt)(nu)gives(pa)(sc)(tn)(un), so the result ispasctnun. -
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
familyas(fa)(mi)(ly)gives(af)(im)(ly), so the result isafimly. -
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
apcsntnuintoapcsandntnuand interleaving the halves givesanptcnsu.
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
atoz - \(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:
- Operation 2:
facebookbecomesfbaocoek. - Operation 0:
fbaocoekbecomesbfoaocke. - Operation 2:
bfoaockebecomesbofcokae. - Operation 1:
bofcokaebecomesbocfkoae.
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.
Log in to write and submit code.
Log in