4 public class funny_rs
implements Runnable
{
5 private BufferedReader in
;
6 private PrintWriter out
;
9 private Set
<String
> answer
, originalWords
;
11 private void check(boolean expr
, String msg
) {
17 private void checkBounds(int n
, int low
, int hi
, String nStr
) {
18 check((low
<= n
) && (n
<= hi
), nStr
+ " is not in [" + low
+ ", " + hi
+ "]");
21 private class Combination
implements Comparable
<Combination
> {
23 public int count
, hash
;
25 public Combination(int[] letters
) {
26 this.letters
= new int[26];
27 for (int i
= 0; i
< 26; i
++) {
28 this.letters
[i
] = letters
[i
];
33 for (int i
= 0; i
< m
; i
++) {
34 for (int j
= 0; j
< 26; j
++) {
35 if (w
[i
][j
] < letters
[j
]) {
43 for (int i
= 0; i
< 26; i
++) {
44 hash ^
= (letters
[i
] << i
);
48 public void genWords(String prefix
) {
49 if (answer
.size() == n
) {
52 boolean found
= false;
53 for (int i
= 0; i
< 26; i
++) {
56 genWords(prefix
+ (char) ('A' + i
));
62 if (prefix
.length() > 0 && !originalWords
.contains(prefix
)) {
69 public int compareTo(Combination that
) {
70 return that
.count
- count
;
74 public boolean equals(Object o
) {
75 Combination that
= (Combination
) o
;
76 for (int i
= 0; i
< letters
.length
; i
++) {
77 if (letters
[i
] != that
.letters
[i
]) {
85 public int hashCode() {
90 private void solve() throws IOException
{
91 StringTokenizer st
= new StringTokenizer(in
.readLine());
92 n
= Integer
.parseInt(st
.nextToken());
93 m
= Integer
.parseInt(st
.nextToken());
94 checkBounds(n
, 1, 100, "n");
95 checkBounds(m
, 1, 1000, "m");
97 originalWords
= new HashSet
<String
>();
98 for (int i
= 0; i
< m
; i
++) {
99 String line
= in
.readLine();
100 originalWords
.add(line
);
101 check(line
.length() <= 100, "word length > 100");
102 for (int j
= 0; j
< line
.length(); j
++) {
103 int ch
= line
.charAt(j
) - 'A';
104 check((0 <= ch
) && (ch
< 26), "Invalid chars");
109 PriorityQueue
<Combination
> queue
= new PriorityQueue
<Combination
>();
110 Set
<Combination
> visited
= new HashSet
<Combination
>();
112 Combination start
= new Combination(new int[26]);
115 answer
= new HashSet
<String
>();
116 while (answer
.size() < n
) {
117 Combination cur
= queue
.poll();
119 for (int i
= 0; i
< 26; i
++) {
121 Combination newCur
= new Combination(cur
.letters
);
123 if (visited
.contains(newCur
)) {
130 for (String w
: answer
) {
135 public static void main(String
[] args
) {
136 new Thread(new funny_rs()).start();
140 String problem
= getClass().getName().split("_")[0];
142 in
= new BufferedReader(new FileReader(new File(problem
+ ".in")));
143 out
= new PrintWriter(new File(problem
+ ".out"));
147 } catch (IOException e
) {