A=B
시간 제한1초메모리 제한2048 MB
순서가 정해진 치환 규칙을 반복 적용하며, 적용 가능한 규칙이 없거나 5000단계 또는 길이 255를 넘으면 멈추는 문제.
문제
After hours of playing the hit puzzle game A=B, you've become an expert at transforming strings using substitution rules. In A=B, players are challenged to manipulate strings using a minimalistic programming language that consists solely of substitution instructions of the form 'A=B'. Each instruction replaces occurrences of string 'A' with string 'B', allowing players to solve various problems—from simple character replacements to more complex transformations like multiplying binary numbers. Inspired by the game's elegant simplicity and the power of its single operation, you had an epiphany: "I could totally make this myself!"
With newfound inspiration, you decide to build your own version of the A=B interpreter. Your version will follow a strict sequence of substitution rules, carefully applying them one at a time until the string can't change any further—or until it grows out of control! You decide to include safety limits for memory and time, so your interpreter won't run away in an endless loop.
A=B's interpreter works as follows: first, it reads a starting string, as well as an ordered list of rules to apply. Each rule is a string of the form '=', where and are strings of uppercase letters without spaces. They each have at most characters. Note that may be empty.
One step of your interpreter consists of finding the first rule, =, such that the string occurs in the current string. Then your interpreter should replace the earliest occurrence of with in the current string.
If your interpreter does not find any rule that can be applied, then it should exit and output the current string.
If, after steps, it is still possible to apply some rule, then your interpreter should output "Time Limit Exceeded" and exit immediately.
If at any point the length of the current string exceeds characters, your interpreter should immediately output "Memory Limit Exceeded" and exit immediately.
입력
The first line contains the starting string ().
The second line contains an integer (), the number of substitution rules.
The next lines each contain a substitution rule of the form '=' (), where and are strings containing only uppercase English letters.
출력
Print "Time Limit Exceeded" if the interpreter could perform more than transformations.
Print "Memory Limit Exceeded" if the string length exceeds characters at any point.
Otherwise, print the value of the final string.