Word
Time limit1sMemory limit128 MB
Apply a cyclic cellular rewriting rule s times to a binary word of length n, then print the lexicographically smallest rotation.
- Level
Medium7 of 10
- Topics
- String, Simulation, Math, Brute force
- Solved
- No attempts yet
Problem
Dr. Wright's class is studying a modified L-system. Here are the details you need.
Consider words of length over the two-letter alphabet . Each word is cyclic: it can be written in any of its cyclic-shift forms, and the first and last letters are treated as neighbours.
A rewriting rule replaces the letter at a position based on the letters at positions , , and (indices are taken cyclically). In one step, all letters of the word are rewritten simultaneously.
Given a starting word and a set of rewriting rules, determine how the word looks after rewriting steps.
Input
The input contains several blocks, each describing one system.
- The first line holds an integer with , the length of the word.
- The second line holds the starting word, made up only of the lowercase letters
aandb. - Each of the next eight lines holds four characters describing one rewriting rule: if the letter at position is , the letter at position is , and the letter at position is , then after rewriting the letter at position becomes . The eight rules are correct and complete (they cover every combination of ).
- The last line of the block holds an integer with .
Process blocks until the end of input.
Output
For each block, print one line containing the word obtained after rewriting steps. Because the word is cyclic it can be written in any of its shifted forms; print the lexicographically smallest such form, assuming a < b.