码迷,mamicode.com
首页 > 其他好文 > 详细

LA 3907 Puzzle

时间:2016-07-22 16:09:01      阅读:301      评论:0      收藏:0      [点我收藏+]

标签:

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:
(1) The players will be given an integer n. Then they should use the first n capital letters from the
Roman alphabet. For example, if n = 4, the four characters A, B, C, and D will be used to solve
this puzzle.
(2) The players will be given s forbidden strings. No forbidden string contains another forbidden
string as a substring. The winner is the student who makes the longest string that does not
include a forbidden string as a substring.
(3) If such a longest string does not exist, i.e., if we can make arbitrarily long strings that satisfy the
above condition, or if we cannot make any string that satisfies the above condition, ‘No’ should
be answered.
For example, suppose the given number n = 2, i.e., the players can use the two characters A 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 is AA. But if the given forbidden strings are
{AAA, BBB, ABAB, BBAA}, we cannot make such a longest string since arbitrarily long concatenations of
ABA, i.e., ABAABAABA . . . do not include any forbidden string.
Input
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 integers
n (1 ≤ n ≤ 26) and s (1 ≤ s ≤ 1, 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.
Output
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.
Sample Input
3
2 4
AAA
AB
BA
BB
2 4
AAA
BBB
ABAB
BBAA
3 7
AA
ABA
BAC
BB
BC
CA
CC
Sample Output
AA
No
ACBAB

 

LA 3907 Puzzle

标签:

原文地址:http://www.cnblogs.com/astoninfer/p/5695522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!