#P10526. [XJTUPC 2024] 命令行

[XJTUPC 2024] 命令行

Problem Description

There are nn pairwise distinct command strings tit_i (1in1 \leq i \leq n), each consisting only of lowercase letters. You need to implement a command line that supports command auto-completion.

Your input area is a string ss. It can accept lowercase letters ’a’-’z’\text{'a'-'z'}, the Tab\text{Tab} key (denoted by ’T’\text{'T'}), the Enter\text{Enter} key (denoted by ’E’\text{'E'}), and the Backspace\text{Backspace} key (denoted by ’B’\text{'B'}). The rules are as follows:

  1. When you receive a lowercase letter cc, append cc to the end of the input area string ss.

  2. When you receive the Backspace\text{Backspace} key, it will try to delete the last character:

    • If the input area string ss is empty, do nothing.
    • If the input area string ss is not empty, discard the last character of ss.
  3. When you receive the Tab\text{Tab} key, perform one smart completion on ss. The completion rules are:

    • Let the set of command strings with prefix ss be TT.
    • If TT is empty, do nothing.
    • If TT is not empty, set ss to lcp(T)\text{lcp}(T). Here lcp\text{lcp} means the longest common prefix, i.e., the longest string that is a prefix of every string in TT.
  4. When you receive the Enter\text{Enter} key, it will try to execute the command ss:

    • If there exists a command string ti=st_i = s, output the command index ii.
    • If there is no command string ti=st_i = s, output 1-1.
    • Whether the execution succeeds or not, clear the input area ss.

Given the input string pp that you receive, you need to output the result of each Enter\text{Enter} key execution.

Input Format

The first line contains two integers nn and mm (1n105,1m5×1061 \leq n \leq 10^5, 1 \leq m \leq 5 \times 10^6), the number of command strings and the length of the input string, separated by spaces.

The next nn lines each contain a string tit_i consisting only of lowercase letters, representing a command string. It is guaranteed that i=1nti106\sum_{i=1}^{n} |t_i| \leq 10^6 and all command strings are pairwise distinct.

The last line contains a string pp (p=m|p|=m), representing the input string. It is guaranteed that pp consists only of {’a’-’z’, ’T’, ’B’, ’E’}\text{\{'a'-'z', 'T', 'B', 'E'\}}.

Output Format

Given the input string pp that you receive, you need to output the result of each Enter\text{Enter} key execution.

7 40
kill
killall
rm
rmdir
ifconfig
ifdown
ll
kTBlEkTaTEiTcBdTElTExjtuTExjtuBBBBBrTdTE

1 2 6 7 -1 4 

Hint

Initially, the input area string s=""s="".

After inputting ’k’\text{'k'}, the input area string becomes s="k"s="k".

After inputting ’T’\text{'T'}, perform smart completion. At this time, T={"kill","killall"}T=\{"kill","killall"\}, and lcp(T)="kill"\text{lcp}(T)="kill". Therefore, the input area string becomes s="kill"s="kill".

After inputting ’B’\text{'B'}, the input area string becomes s="kil"s="kil".

After inputting ’l’\text{'l'}, the input area string becomes s="kill"s="kill".

After inputting ’E’\text{'E'}, since t1=st_1=s, you should output 11.

Translated by ChatGPT 5