about summary refs log tree commit diff
path: root/12/TP-HN-2009/R2/BAI3.PAS
blob: 30eecdfc0835c8b91d36a3643a8452302f9b3579 (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
type
  ar = array of ansistring;

var
  f : text;
  s0 : ansistring;
  a0 : ar;

function incre(s1, s2 : ansistring) : boolean;
  var
    i : smallint;
  begin
    if length(s1) < length(s2) then exit(true);
    if length(s1) > length(s2) then exit(false);
    for i := 1 to length(s1) do
      begin
        if ord(s1[i]) < ord(s2[i]) then exit(true);
        if ord(s1[i]) > ord(s2[i]) then exit(false)
      end;
    exit(false)
  end;

function cal(a : ar) : smallint;
  var
    len, i, tmp : smallint;
  begin
    cal := 0;
    i := 0;
    len := length(a);
    while (cal + i < len) and (i + 1 < len) do
      begin
        inc(i);
        if incre(a[0], a[i]) then
          begin
            tmp := cal(copy(a, i, len - i)) + 1;
            if tmp > cal then
              cal := tmp
          end
      end
  end;

function putin(
  aray : ar;
  strng : ansistring;
  len : smallint
) : ar;
  begin
    setlength(aray, length(aray) + 1);
    aray[length(aray) - 1] := copy(strng, 1, len);
    exit(aray)
  end;

function libai3(
  s : ansistring;
  a : ar;
  n : smallint
) : smallint;
  var
    len, i, tmp : smallint;
  begin
    if s = '' then
      exit(cal(a));
    libai3 := 0;
    len := length(s);
    i := 0;
    while (i < n) and (i + libai3 < len) do
      begin
        inc(i);
        tmp := libai3(copy(s, i + 1, len - i), putin(a, s, i), n + 1);
        if tmp > libai3 then
          libai3 := tmp
      end
  end;

begin
  assign(f, 'BAI3.INP');
  reset(f);
  readln(f);
  read(f, s0);
  close(f);
  setlength(a0, 1);
  a0[0] := '';
  assign(f, 'BAI3.OUT');
  rewrite(f);
  writeln(f, libai3(s0, a0, 1));
  close(f)
end.