4 public class funny_rs_tl
implements Runnable
{
5 private BufferedReader in
;
6 private PrintWriter out
;
9 private boolean[][] mark
;
11 private Set
<String
> answer
, originalWords
;
13 private void check(boolean expr
, String msg
) {
19 private void checkBounds(int n
, int low
, int hi
, String nStr
) {
20 check((low
<= n
) && (n
<= hi
), nStr
+ " is not in [" + low
+ ", " + hi
+ "]");
23 private void gen(int minWords
, int pos
) {
26 for (int i
= 0; i
< m
; i
++) {
31 if (good
> minWords
) {
37 for (int u
= 0; ; u
++) {
40 for (int i
= 0; i
< m
; i
++) {
41 mark
[pos
+ 1][i
] = mark
[pos
][i
] && (w
[i
][pos
] >= u
);
42 if (mark
[pos
+ 1][i
]) {
46 if (good
< minWords
) {
49 gen(minWords
, pos
+ 1);
50 if (answer
.size() == n
) {
56 private void genWords(String prefix
) {
57 if (answer
.size() == n
) {
60 boolean found
= false;
61 for (int i
= 0; i
< 26; i
++) {
64 genWords(prefix
+ (char) ('A' + i
));
70 if (prefix
.length() > 0 && !originalWords
.contains(prefix
)) {
76 private void solve() throws IOException
{
77 StringTokenizer st
= new StringTokenizer(in
.readLine());
78 n
= Integer
.parseInt(st
.nextToken());
79 m
= Integer
.parseInt(st
.nextToken());
80 checkBounds(n
, 1, 100, "n");
81 checkBounds(m
, 1, 1000, "m");
83 originalWords
= new HashSet
<String
>();
84 for (int i
= 0; i
< m
; i
++) {
85 String line
= in
.readLine();
86 originalWords
.add(line
);
87 check(line
.length() <= 100, "word length > 100");
88 for (int j
= 0; j
< line
.length(); j
++) {
89 int ch
= line
.charAt(j
) - 'A';
90 check((0 <= ch
) && (ch
< 26), "Invalid chars");
95 mark
= new boolean[27][m
];
97 answer
= new HashSet
<String
>();
98 for (int i
= m
; i
>= 0; i
--) {
99 if (answer
.size() < n
) {
100 Arrays
.fill(mark
[0], true);
104 for (String w
: answer
) {
109 public static void main(String
[] args
) {
110 new Thread(new funny_rs_tl()).start();
114 String problem
= getClass().getName().split("_")[0];
116 in
= new BufferedReader(new FileReader(new File(problem
+ ".in")));
117 out
= new PrintWriter(new File(problem
+ ".out"));
121 } catch (IOException e
) {