Jisung is the student representative of the Department of Computer Engineering in ACM University. A few days later, the annual festival will be held for the students in the department. He is preparing some events for the festival. Since Jisung likes to make and solve puzzles, he decides to devise some interesting puzzle for the festival.
The followings are the rules for the puzzle made by Jisung:
For example, suppose the given number n = 2, i.e., the players can use the two charactersA and B. Assume that the forbidden strings are {AAA, AB, BA, BB}. In this case, the longest string that does not include any of the four forbidden strings as substrings isAA. But if the given forbidden strings are {AAA, BBB, ABAB, BBAA}, we cannot make such a longest string since arbitrarily long concatenations ofABA, i.e., ABAABAABA ... do not include any forbidden string.
Your program is to read from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integersn (1n26) and s (1s1, 000) which represent the number of characters and the number of forbidden strings, respectively. From the second line to(s + 1)-st line of the test case, the forbidden strings are given one by one. The length of a forbidden string does not exceed 50.
Your program is to write to standard output. Print exactly one line for each test case. Print the longest string that does not include any forbidden string as a substring if it exists, otherwise, just print `No' as output. When there exists more than one such longest string with the same length, print the lexicographically largest string among them.
3 2 4 AAA AB BA BB 2 4 AAA BBB ABAB BBAA 3 7 AA ABA BAC BB BC CA CC
AA No ACBAB
找一个串包含A到A+N,这个串不包括给的一些子串,使这个串最长。如果不存在或无限长输出No。
先用子串建立AC自动机,只要是包含那些子串的串都标记,val[u]|=val[f[u]]。这样问题变成了搜索一个最长的串不经过标记节点,并且不出现环。
dp[u]表示在AC自动机图上从u这个点开始(不包括u)能找到的最长串,如果一个点在之前已经被访问过,也就是说dp[u]!=-1,就直接返回。那会不会u开始的最长串和当前搜索到u的这个串出现环呢?是不会的,因为u的最长串如果经过搜索到u之前的某个点,那个点的dp[u]!=-1,这个串搜到那个点的时候就已经返回了,不会搜到u了。
用vis表示当前路径上的点,只要在搜索过程中发现环,答案就是No了。
更新dp的时候记录路径,如果不是No的话打印路径。
#include
#include
#include
#include
#include
#include
#include