创建缩写AI (CI翻译)
The Beaver was given a set of words $S$, which initially contained $n$ words. Then he performed the following operation $m$ times:
海狸得到一个单词集合 $S$,其中最初包含 $n$ 个单词。随后,他执行以下操作 $m$ 次:AI (CI翻译)
- The Beaver forms a sequence of one or more words from the set
$S$. The same word may appear in the sequence several times. An abbreviation$^{\text{∗}}$is formed from the resulting phrase. - Then the Beaver adds the resulting abbreviation to
$S$and can now use it in subsequent operations as an ordinary word.
- 海狸从集合
$S$中选出由一个或多个单词组成的序列。同一个单词可以在序列中出现多次。由所得短语构成一个缩写$^{\text{∗}}$。 - 然后,海狸将所得缩写加入
$S$,之后便可以在后续操作中将其作为普通单词使用。
You are given the $n$ initial words that were in the set $S$, and the set of abbreviations that the Beaver formed. Determine whether the Beaver made a mistake and whether all these abbreviations could have appeared as a result of the operation described above. Note that the abbreviations did not necessarily appear in the same order in which they are given to you.
给定最初在集合 $S$ 中的 $n$ 个单词,以及海狸构成的缩写集合。请判断海狸是否犯了错误,即这些缩写是否都可能通过上述操作得到。注意,这些缩写不一定按照提供给你的顺序出现。AI (CI翻译)
$^{\text{∗}}$The abbreviation of a sequence is the word produced by the first letters of the words in the sequence. For example, the sequence birch OAK birch redwood produces the abbreviation BOBR.
$^{\text{∗}}$一个序列的缩写是由该序列中各个单词的首字母组成的单词。例如,序列 birch OAK birch redwood 会产生缩写 BOBR。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 500$)。接下来是各个测试用例的描述。AI (CI翻译)
The first line of each test case contains two integers $n$ and $m$ — the number of ordinary words and the number of abbreviations, respectively ($1 \le n, m \le 100$).
每个测试用例的第一行包含两个整数 $n$ 和 $m$——普通单词和缩写的数量,分别为 $n$ 和 $m$($1 \le n, m \le 100$)。AI (CI翻译)
Each of the next $n$ lines contains one string $w_i$ — an ordinary word ($1 \le |w_i| \le 20$).
接下来的 $n$ 行中,每行包含一个字符串 $w_i$——一个普通单词($1 \le |w_i| \le 20$)。AI (CI翻译)
Each of the next $m$ lines contains one string $a_i$ — an abbreviation formed by Bobr ($1 \le |a_i| \le 20$).
接下来的 $m$ 行中,每行包含一个字符串 $a_i$——由 Bobr 形成的缩写($1 \le |a_i| \le 20$)。AI (CI翻译)
All ordinary words consist of lowercase English letters, and all abbreviations consist of uppercase English letters. In each test case, all strings $w_1, w_2, \ldots, w_n, a_1, a_2, \ldots, a_m$ are pairwise distinct.
所有普通单词均由小写英文字母组成,所有缩写均由大写英文字母组成。在每个测试用例中,所有字符串 $w_1, w_2, \ldots, w_n, a_1, a_2, \ldots, a_m$ 两两不同。AI (CI翻译)
The total length of all strings over all test cases does not exceed $50\,000$.
所有测试用例中所有字符串的总长度不超过 $50\,000$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, print "YES" if there exists a suitable order in which the given abbreviations could have appeared, and "NO" otherwise.
对于每个测试用例,输出如果存在一种合适的顺序,使给出的缩写能够按照该顺序出现,则输出 "YES";否则输出 "NO"。AI (CI翻译)
You may print each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
每个字母的大小写均可任意输出(小写或大写均可)。例如,字符串 "yEs"、"yes"、"Yes" 和 "YES" 都会被接受为肯定答案。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, the order AG, BG, CG, ABC is suitable. The first three abbreviations can be created using the pairs of words apple and grand, banana and great, and cherry and good, respectively. After that, the abbreviation ABC can be created using the already created abbreviations AG, BG, and CG.
在第一个测试用例中,顺序 AG、BG、CG、ABC 是合适的。前三个缩写可以分别使用单词对 apple 和 grand、banana 和 great、cherry 和 good 构成。之后,可以使用已经构成的缩写 AG、BG 和 CG 构成缩写 ABC。AI (CI翻译)
In the second test case, one can create AA using apple twice.
在第二个测试用例中,可以使用 apple 两次构成 AA。AI (CI翻译)
In the third test case, one can first create A using apple, and then create AA using apple and the already created abbreviation A.
在第三个测试用例中,可以先使用 apple 构成 A,然后使用 apple 和已经构成的缩写 A 构成 AA。AI (CI翻译)
In the fourth test case, it can be shown that the required order of creating the abbreviations does not exist.
在第四个测试用例中,可以证明不存在所需的缩写构成顺序。AI (CI翻译)
样例
4
6 4
apple
grand
banana
great
cherry
good
AG
BG
CG
ABC
1 1
apple
AA
1 2
apple
A
AA
2 2
apple
avocado
B
BA
YES
YES
YES
NO
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。