1 /* Generates all short words and chooses best */
4 import java
.io
.IOException
;
5 import java
.io
.PrintWriter
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collections
;
8 import java
.util
.HashSet
;
10 import java
.util
.Scanner
;
12 import java
.util
.TreeSet
;
14 public class funny_gk_wa_short
{
16 static PrintWriter out
;
18 static int MAX_N
= 100;
19 static int MAX_M
= 1000;
21 static final int MAX_W
= 100;
22 static final int MIN_CH
= 'A';
23 static final int MAX_CH
= 'Z';
27 static class Entry
implements Comparable
<Entry
> {
31 Entry(int count
, String word
) {
37 public int compareTo(Entry that
) {
38 return that
.count
- this.count
;
42 private int count(String word
) {
43 int[] letters
= letters(word
);
45 for (int[] wordLetters
: letterss
) {
46 if (match(letters
, wordLetters
)) {
53 private boolean match(int[] letters
, int[] wordLetters
) {
54 for (int i
= 0; i
< 26; i
++) {
55 if (letters
[i
] > wordLetters
[i
]) {
62 private int[] letters(String word
) {
63 int[] letters
= new int[26];
64 for (char ch
: word
.toCharArray()) {
65 letters
[ch
- MIN_CH
]++;
71 public void run() throws IOException
{
75 assert 1 <= n
&& n
<= MAX_N
;
76 assert 1 <= m
&& m
<= MAX_M
;
78 Set
<String
> words
= new HashSet
<String
>();
79 for (int i
= 0; i
< m
; i
++) {
80 String word
= in
.next();
83 assert 1 <= word
.length() && word
.length() <= MAX_W
;
84 for (char ch
: word
.toCharArray()) {
85 assert MIN_CH
<= ch
&& ch
<= MAX_CH
;
89 Set
<Character
> letters
= new HashSet
<Character
>();
91 letterss
= new ArrayList
<int[]>();
92 for (String word
: words
) {
93 letterss
.add(letters(word
));
94 for (char ch
: word
.toCharArray()) {
99 int maxWords
= 2000000 / words
.size();
100 List
<Entry
> queue
= new ArrayList
<Entry
>();
101 queue
.add(new Entry(0, ""));
103 while (head
< queue
.size() && queue
.size() < maxWords
) {
104 String word
= queue
.get(head
++).word
;
105 for (int i
= 0; i
< 26; i
++) {
106 String nword
= word
+ (char) ('A' + i
);
107 int count
= count(nword
);
109 queue
.add(new Entry(count
, nword
));
113 Collections
.sort(queue
);
115 Set
<String
> result
= new TreeSet
<String
>();
116 while (head
< queue
.size() && result
.size() < n
) {
117 String word
= queue
.get(head
++).word
;
118 if (word
.length() > 0 && !words
.contains(word
)) {
123 StringBuilder sb
= new StringBuilder();
124 for (int i
= 0; result
.size() < n
; i
++) {
126 String word
= sb
.toString();
127 if (!words
.contains(word
)) {
132 for (String word
: result
) {
137 public static void main(String
[] args
) throws Exception
{
138 in
= new Scanner(new File("funny.in"));
139 out
= new PrintWriter("funny.out");
141 new funny_gk_wa_short().run();