1 import java
.io
.BufferedReader
;
2 import java
.io
.FileReader
;
3 import java
.io
.PrintWriter
;
4 import java
.io
.IOException
;
7 public class funny_petr
implements Runnable
{
10 static class State
implements Comparable
<State
> {
11 int[] cnt
= new int[26];
20 public boolean equals(Object o
) {
21 if (this == o
) return true;
22 if (o
== null || getClass() != o
.getClass()) return false;
24 State state
= (State
) o
;
26 if (!Arrays
.equals(cnt
, state
.cnt
)) return false;
32 public int hashCode() {
33 int result
= cnt
!= null ? Arrays
.hashCode(cnt
) : 0;
37 public int compareTo(State state
) {
38 int z
= -score
+ state
.score
;
41 for (int i
= 0; i
< 26; ++i
)
42 if (cnt
[i
] != state
.cnt
[i
])
43 return cnt
[i
] - state
.cnt
[i
];
47 public int getScore() {
49 for (int i
= 0; i
< wcnt
.length
; ++i
) {
51 for (int j
= 0; j
< 26; ++j
)
52 if (cnt
[j
] > wcnt
[i
][j
]) {
65 private void solve() throws IOException
{
69 String
[] w
= new String
[m
];
70 for (int i
= 0; i
< m
; ++i
) {
73 wcnt
= new int[m
][26];
74 for (int i
= 0; i
< m
; ++i
)
75 for (int j
= 0; j
< w
[i
].length(); ++j
)
76 ++wcnt
[i
][w
[i
].charAt(j
) - 'A'];
77 TreeSet
<State
> queue
= new TreeSet
<State
>();
78 queue
.add(new State(new int[26]));
79 Set
<String
> got
= new HashSet
<String
>();
81 got
.addAll(Arrays
.asList(w
));
82 List
<String
> sols
= new ArrayList
<String
>();
83 while (sols
.size() < n
) {
84 State cur
= queue
.first();
86 addSols(got
, sols
, cur
);
89 for (int i
= 0; i
< 26; ++i
) {
90 int[] z
= cur
.cnt
.clone();
92 State s
= new State(z
);
100 private void addSols(Set
<String
> got
, List
<String
> sols
, State cur
) {
101 rec(got
, sols
, cur
.cnt
.clone(), "");
104 private boolean rec(Set
<String
> got
, List
<String
> sols
, int[] cnt
, String prefix
) {
106 for (int i
= 0; i
< 26; ++i
)
109 if (rec(got
, sols
, cnt
, prefix
+ (char) ('A' + i
)))
115 if (!got
.contains(prefix
)) {
117 if (sols
.size() >= n
)
125 public static void main(String
[] args
) throws InterruptedException
{
126 Thread thread
= new Thread(new funny_petr());
131 static final String TASK_ID
= "funny";
132 static final String IN_FILE
= TASK_ID
+ ".in";
133 static final String OUT_FILE
= TASK_ID
+ ".out";
134 BufferedReader reader
;
135 StringTokenizer tokenizer
;
140 reader
= new BufferedReader(new FileReader(IN_FILE
));
142 writer
= new PrintWriter(OUT_FILE
);
146 } catch (Exception e
) {
152 int nextInt() throws IOException
{
153 return Integer
.parseInt(nextToken());
156 long nextLong() throws IOException
{
157 return Long
.parseLong(nextToken());
160 double nextDouble() throws IOException
{
161 return Double
.parseDouble(nextToken());
164 String
nextToken() throws IOException
{
165 while (tokenizer
== null || !tokenizer
.hasMoreTokens()) {
166 tokenizer
= new StringTokenizer(reader
.readLine());
168 return tokenizer
.nextToken();