Advertisement
savrasov

strings

Nov 28th, 2016
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.08 KB | None | 0 0
  1. uses
  2.   SysUtils;
  3.  
  4. type
  5.   arr = record
  6.     a: array [0..25000] of integer;
  7.   end;
  8.  
  9. var
  10.   s: array [1..25000] of string;
  11.   boo: array [1..25000] of boolean;
  12.   i, k, j, t, h: integer;
  13.   ff: arr;
  14.   f: array [1..5, 0..25000] of integer;
  15.  
  16. function check(s, s1: string): boolean;
  17. begin
  18.   if (length(s) <> length(s1)) then check := false
  19.   else
  20.   begin
  21.     while (length(s) > 0) and (pos(s[1], s1) <> 0) do
  22.     begin
  23.       delete(s1, pos(s[1], s1), 1);
  24.       delete(s, 1, 1);
  25.     end;
  26.     check := (length(s) = 0);
  27.   end;
  28. end;
  29.  
  30. function sr(s, s1: string): boolean;
  31. begin
  32.   while (length(s) > 0) and (length(s1) > 0) and (s[1] = s1[1]) do
  33.   begin
  34.     delete(s, 1, 1);
  35.     delete(s1, 1, 1);
  36.   end;
  37.   if (length(s) = 0) then sr := true
  38.   else if (length(s1) = 0) then sr := false
  39.   else sr := (ord(s[1]) < ord(s1[1]));
  40. end;
  41.  
  42. procedure swap(var a, b: integer);
  43. var
  44.   c: integer;
  45. begin
  46.   c := a;
  47.   a := b;
  48.   b := c;
  49. end;
  50.  
  51. function sort(f: arr; k: integer): arr;
  52. var
  53.   i, j: integer;
  54. begin
  55.   for i := 1 to k do
  56.   for j := 2 to k do
  57.     if (f.a[j - 1] <> 0) and (f.a[j] <> 0) and (not sr(s[f.a[j - 1]], s[f.a[j]])) then swap(f.a[j], f.a[j - 1]);
  58.   sort := f;
  59. end;
  60.  
  61. begin
  62.   i := 1;
  63.   while not eof do
  64.   begin
  65.     readln(s[i]);
  66.     inc(i);
  67.   end;
  68.   k := i - 1;
  69.   for i := 1 to k do
  70.   begin
  71.     if not boo[i] then
  72.     begin
  73.       t := 1;
  74.       for j := 1 to k do
  75.       if (check(s[i], s[j])) and not boo[j] then
  76.       begin
  77.         ff.a[t] := j;
  78.         boo[j] := true;
  79.         inc(t);
  80.       end;
  81.       ff := sort(ff, t - 1);
  82.       ff.a[0] := t - 1;
  83.       for h := 1 to 5 do
  84.       if (f[h, 0] = 0) or((ff.a[0] > f[h, 0]) or ((ff.a[0] = f[h, 0]) and ( sr(s[ff.a[1]], s[f[h, 1]])))) then
  85.       begin
  86.         for j := 4 downto h do f[j + 1] := f[j];
  87.         for j := 1 to ff.a[0] do f[h, j] := ff.a[j];
  88.         f[h, 0] := ff.a[0];
  89.         break;
  90.       end;
  91.     end;
  92.   end;
  93.   for i := 1 to 5 do
  94.   if f[i, 0] <> 0 then
  95.   begin
  96.     write('Group of size ', f[i, 0], ': ');
  97.     for j := 1 to f[i, 0] do write(s[f[i, j]], ' ');
  98.     writeln('.');
  99.   end;
  100. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement