5 * @author Iskander Akishev
7 public class funny_ia
implements Runnable
{
9 private static final String FILE_NAME
= "funny";
11 private TreeSet
<String
> words
;
12 private TreeSet
<NewWord
> newWords
;
20 } catch (Throwable t
) {
26 private BufferedReader in
;
27 private PrintWriter out
;
29 private void solve() throws Exception
{
30 in
= new BufferedReader(new FileReader(FILE_NAME
+ ".in"));
31 out
= new PrintWriter(new File(FILE_NAME
+ ".out"));
33 StringTokenizer st
= new StringTokenizer(in
.readLine());
34 n
= Integer
.parseInt(st
.nextToken());
35 m
= Integer
.parseInt(st
.nextToken());
38 words
= new TreeSet
<String
>();
39 for (int i
= 0; i
< m
; i
++) {
40 String word
= in
.readLine();
42 for (char c
: word
.toCharArray()) {
50 newWords
= new TreeSet
<NewWord
>();
53 NewWord cur
= newWords
.first();
55 for (int k
= 0; k
< lim
; k
++) {
56 for (char c
= 'A'; c
<= 'Z'; c
++) {
59 cur
= newWords
.higher(cur
);
66 newWords
.remove(newWords
.first());
68 for (NewWord nw
: newWords
) {
69 if (words
.contains(nw
.word
)) {
83 int[] a
= new int[26];
85 private void add(String s
) {
87 for (int i
= 0; i
< s
.length(); i
++) {
92 LOOP
: for (int[] ww
: w
) {
93 for (int i
= 0; i
< 26; i
++) {
100 newWords
.add(new NewWord(s
, cnt
));
101 if (newWords
.size() > lim
) {
102 newWords
.remove(newWords
.last());
106 static class NewWord
implements Comparable
<NewWord
> {
110 NewWord(String word
, int score
) {
115 public int compareTo(NewWord o
) {
116 int res
= o
.score
- score
;
118 res
= word
.compareTo(o
.word
);
124 public static void main(String
[] args
) {
125 new Thread(new funny_ia()).start();