#P16268. [蓝桥杯 2026 省 Java B 组] 游戏指令解析器

[蓝桥杯 2026 省 Java B 组] 游戏指令解析器

Problem Description

An adventure game maintains a command library that contains nn distinct full commands.

The player will input mm 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 ss, define its matching result with the command library as follows:

  • If there exists exactly one full command cc such that ss is a prefix of cc, then ss is considered to uniquely match cc. The system executes that command and outputs cc.
  • If two or more full commands use ss as a prefix, then ss is considered a multiple match, and output ambiguous.
  • If there is no full command in the library that has ss as a prefix, then ss 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,mn, m, representing the number of full commands in the command library and the number of input commands from the player.

The next nn lines each contain a string, representing a full command.

The next mm lines each contain a string, representing an input command.

Output Format

Output mm lines. The ii-th line is the matching result for the ii-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.

  • 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%30\% of the testdata, n10n \leq 10 and m10m \leq 10.

For 60%60\% of the testdata, n50n \leq 50 and m50m \leq 50.

For all testdata, 1n1001 \leq n \leq 100 and 1m1001 \leq m \leq 100.

It is guaranteed that the lengths of all command names and input commands are within [1,20][1, 20], all strings contain only lowercase English letters, and all full commands in the command library are distinct.

Translated by ChatGPT 5