#P16268. [蓝桥杯 2026 省 Java B 组] 游戏指令解析器
[蓝桥杯 2026 省 Java B 组] 游戏指令解析器
Problem Description
An adventure game maintains a command library that contains distinct full commands.
The player will input 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 , define its matching result with the command library as follows:
- If there exists exactly one full command such that is a prefix of , then is considered to uniquely match . The system executes that command and outputs .
- If two or more full commands use as a prefix, then is considered a multiple match, and output
ambiguous. - If there is no full command in the library that has as a prefix, then 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 , representing the number of full commands in the command library and the number of input commands from the player.
The next lines each contain a string, representing a full command.
The next lines each contain a string, representing an input command.
Output Format
Output lines. The -th line is the matching result for the -th input command:
- Unique match: output the corresponding full command.
- Multiple match: output
ambiguous. - Unable to match: output
unknown.
6 5
attack
defend
move
magic
examine
exit
att
def
mov
ex
e
attack
defend
move
ambiguous
ambiguous
4 4
open
close
operate
unlock
op
clo
un
cast
ambiguous
close
unlock
unknown
Hint
Sample Explanation 1
The command library is: attack, defend, move, magic, examine, exit.
attis a prefix ofattackand matches only this one, so outputattack.defmatches onlydefend, so outputdefend.movmatches onlymove, so outputmove.exmatches bothexamineandexit, so it is a multiple match, outputambiguous.ematches bothexamineandexit, so outputambiguous.
Sample Explanation 2
opmatches bothopenandoperate, so outputambiguous.clomatches onlyclose, so outputclose.unmatches onlyunlock, so outputunlock.castcannot match any full command, so outputunknown.
Constraints
For of the testdata, and .
For of the testdata, and .
For all testdata, and .
It is guaranteed that the lengths of all command names and input commands are within , all strings contain only lowercase English letters, and all full commands in the command library are distinct.
Translated by ChatGPT 5