TORIE
Time limit1sMemory limit1024 MB
Given nested bracket trees with uppercase labels, remove the fewest TORIEs when the keyword matches a label or a label extended into child labels, then print what remains.
- Level
Medium6 of 10
- Topics
- Tree, String matching, DFS
- Solved
- No attempts yet
Problem
A TORIE is a string of the form [ABC], made of an opening bracket [, one or more uppercase English letters, and a closing bracket ]. A TORIE may have child TORIEs. When a TORIE has a child TORIE, the child appears right after the uppercase string. [A[B][C]] represents an A TORIE whose children are a B TORIE and a C TORIE. A child TORIE has the same properties: it is wrapped in brackets, and it may have children of its own.
Problem
You can remove TORIEs with a keyword in the following ways.
- If a substring that starts at some position in a TORIE's uppercase string matches the keyword, you can remove that TORIE.
- If the longest substring that can be formed from some position in a TORIE's uppercase string is shorter than the keyword, you can extend the substring into the uppercase string of one of its child TORIEs. If the same condition holds again, you can continue from that child into its own child. If the extended substring matches the keyword, you can remove the TORIE where the substring started.
- You can remove TORIEs with the keyword many times. When a TORIE is removed, all of its child TORIEs are removed too.
Using the keyword, remove TORIEs so that as few TORIEs as possible remain. Then print the remaining TORIEs in the order they were given. If every TORIE is removed, print torie!.
Input
The first line contains a string that represents one or more TORIEs. The total length of the string is at least 3 and at most . The input is always a valid TORIE string.
The second line contains the keyword. The keyword consists only of uppercase English letters, and its length is at least 1 and at most 20.
Output
Print the TORIEs that remain after all removals, in the order they were given. If every TORIE is removed, print torie!.