about summary refs log tree commit diff
path: root/THT/B/QG-2014/guess.pas
blob: af394344896ebb19761c8279b9cf523ec25bbe5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
uses dic;

type
  dic_t = array of string;
  coun_t = array['a'..'z'] of byte;

var
  f: text;
  dict, new_dict: array[0..999999] of string;
  chars: array of coun_t;
  count: coun_t;
  enum: array of record i, n: byte end;
  len, new_len: longint;
  i, j: byte;
  c: char;


procedure swapbyte(var x, y: byte);
  var
    tmp: byte;

  begin
    tmp := x;
    x := y;
    y := tmp
  end;



begin
  len := 0;
  assign(f, 'DIC.DAT');
  reset(f);
  while not eof(f) do
    begin
      readln(f, dict[len]);
      inc(len)
    end;
  close(f);

  setlength(chars, len);
  for i := 0 to len - 1 do
    begin
      for c := 'a' to 'z' do
        chars[i][c] := 0;
      for c in dict[i] do
        inc(chars[i][c])
    end;
  for c := 'a' to 'z' do
    count[c] := count_char(c);

  new_len := 0;
  for i := 0 to len - 1 do
    begin
      for c := 'a' to '{' do
        if chars[i][c] <> count[c] then
          break;
      if c = '{' then
        begin
          new_dict[new_len] := dict[i];
          inc(new_len)
        end
    end;

  setlength(enum, length(new_dict[0]));
  for i := 0 to length(enum) - 1 do
    begin
      enum[i].i := i + 1;
      enum[i].n := 0;
      for c := 'a' to 'z' do
        count[c] := 0;
      for j := 0 to new_len - 1 do
        inc(count[new_dict[j][i + 1]]);
      for c := 'a' to 'z' do
        if count[c] > 0 then
          inc(enum[i].n)
    end;

  for i := 0 to length(enum) - 2 do
    for j := i + 1 to length(enum) - 1 do
      if enum[i].n < enum[j].n then
        begin
          swapbyte(enum[i].n, enum[j].n);
          swapbyte(enum[i].i, enum[j].i)
        end;

  j := 0;
  while new_len > 1 do
    begin
      len := new_len;
      for i := 0 to len - 1 do
        dict[i] := new_dict[i];

      c := get_char_at_pos(enum[j].i);
      new_len := 0;
      for i := 0 to len - 1 do
        if dict[i][enum[j].i] = c then
          begin
            new_dict[new_len] := dict[i];
            inc(new_len)
          end;
      inc(j)
    end;

  answer(new_dict[0])
end.