P16268 [Lanqiao Cup 2026 NOI Qualifier Java Group B] Game Command Parser
Description
An adventure game maintains a command library that contains $n$ distinct full commands.
The player will input $m$ strings one by one as input commands. An input command can be a full command itself, or a prefix of some full command (continuous matching starting from the 1st character).
For each input command $s$, define its matching result with the command library as follows:
- If there exists exactly one full command $c$ such that $s$ is a prefix of $c$, then $s$ is considered to **uniquely match** $c$. The system executes that command and outputs $c$.
- If two or more full commands use $s$ as a prefix, then $s$ is considered a **multiple match**, and output `ambiguous`.
- If there is no full command in the library that has $s$ as a prefix, then $s$ is considered **unable to match**, and output `unknown`.
Now, for each command entered by the player, output its matching result according to the rules above.
Input Format
The first line contains two integers $n, m$, representing the number of full commands in the command library and the number of input commands from the player.
The next $n$ lines each contain a string, representing a full command.
The next $m$ lines each contain a string, representing an input command.
Output Format
Output $m$ lines. The $i$-th line is the matching result for the $i$-th input command:
- Unique match: output the corresponding full command.
- Multiple match: output `ambiguous`.
- Unable to match: output `unknown`.
Explanation/Hint
### Sample Explanation 1
The command library is: `attack`, `defend`, `move`, `magic`, `examine`, `exit`.
- `att` is a prefix of `attack` and matches only this one, so output `attack`.
- `def` matches only `defend`, so output `defend`.
- `mov` matches only `move`, so output `move`.
- `ex` matches both `examine` and `exit`, so it is a multiple match, output `ambiguous`.
- `e` matches both `examine` and `exit`, so output `ambiguous`.
### Sample Explanation 2
- `op` matches both `open` and `operate`, so output `ambiguous`.
- `clo` matches only `close`, so output `close`.
- `un` matches only `unlock`, so output `unlock`.
- `cast` cannot match any full command, so output `unknown`.
### Constraints
For $30\%$ of the testdata, $n \leq 10$ and $m \leq 10$.
For $60\%$ of the testdata, $n \leq 50$ and $m \leq 50$.
For all testdata, $1 \leq n \leq 100$ and $1 \leq m \leq 100$.
It is guaranteed that the lengths of all command names and input commands are within $[1, 20]$, all strings contain only lowercase English letters, and all full commands in the command library are distinct.
Translated by ChatGPT 5