#P17453. AC 自动机 / AC Automaton

    ID: 19966 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>有限状态自动机2026高校校赛

AC 自动机 / AC Automaton

Problem Description

Given a deterministic finite automaton (DFA) with state set Q={1,2,…,n}Q=\{1,2,\dots,n\}, start state q=1q=1, accepting state set FF of size mm, and alphabet Σ={A,C}\Sigma=\{\texttt{A},\texttt{C}\}. Compute the lexicographically smallest string that can be accepted by the automaton and has length at least kk. Since the answer may be very long, if the length of the answer exceeds nn, you only need to output the last nn characters of the answer. If no string satisfying the above requirements exists, output WA.

A string is accepted by the automaton if and only if there exists a walk that starts from the start state and ends at an accepting state, such that the letters on the transition edges traversed form exactly the string. The walk may visit repeated states and repeated edges.

Input Format

This problem contains multiple test cases.

The first line contains an integer TT (1≤T≤5×1051\le T\le 5\times 10^5), indicating the number of test cases.

For each test case:

  • The first line contains three integers separated by spaces: the number of states nn (2≤n≤1062\le n\le 10^6), the number of accepting states mm (1≤m≤n−11\le m\le n-1), and the minimum length kk (1≤k≤1091\le k\le 10^9).
  • The second line contains mm integers separated by spaces, indicating the accepting state set. It is guaranteed that these numbers are all in $[2,n]` and are distinct.
  • The next nn lines each contain 22 integers separated by spaces. In line ii, the two integers represent the state reached from state ii by transitions labeled A and C, respectively.

It is guaranteed that ∑n≤106\sum n\le 10^6.

Output Format

Output TT lines. Each line outputs one string, where the ii-th line is the answer for the ii-th test case.

2
5 1 6
4
2 5
5 3
4 2
5 5
5 5
3 1 2
2
2 3
3 3
3 3
CCCCA
WA

Hint

In the first sample test case, the language recognized by the DFA is $\{\texttt{A}\texttt{C}^{2t+1}\texttt{A}\mid t\ge 0\}$.

Translated by ChatGPT 5