Advertisement
Guest User

Untitled

a guest
May 25th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 2.76 KB | None | 0 0
  1. uses Math, sysutils;
  2. {$mode objfpc}{$H+}
  3.  
  4. type
  5.   myType = record
  6.     s: string;
  7.     rooms: array [1..5000] of integer;
  8.   end;
  9. var
  10.   t1, t2: myType;
  11.   i, j, ans, buf, k, MergeCount, f: integer;
  12.   merge: array of array[1..2] of integer;
  13. begin
  14.   assign(input,'input.txt');
  15.   assign(output,'output.txt');
  16.   reset(input);
  17.   rewrite(output);
  18.   readln(t1.s);
  19.   ans := 0;
  20.   k := 0;
  21.   i := 0;
  22.   MergeCount := 0;
  23.   SetLength(merge, 1);
  24.   while i <= Length(t1.s) do begin
  25.     inc(i);
  26.     if (t1.s[i] = ' ') then begin
  27.       inc(ans);
  28.       inc(k);
  29.       t1.rooms[i] := k;
  30.       while (i <= Length(t1.s)) and (t1.s[i] = ' ') do begin
  31.         t1.rooms[i] := k;
  32.         inc(i);
  33.       end;
  34.     end;
  35.   end;
  36.  
  37.   while not eof(input) do begin
  38.     t2.s := t1.s;
  39.     for i := 1 to Length(t1.s) do begin
  40.       t2.rooms[i] := t1.rooms[i];
  41.       t1.rooms[i] := 0;
  42.     end;
  43.  
  44.     readln(t1.s);
  45.     i := 0;
  46.     while i <= Length(t2.s) do begin
  47.       inc(i);
  48.       if (t2.s[i] = ' ') and (t1.s[i] = ' ') then begin
  49.         if (t1.rooms[i] <> t2.rooms[i]) and (t1.rooms[i] <> 0) then begin
  50.           buf := t2.rooms[i];
  51.  
  52.           f := 0;
  53.           for j := 1 to MergeCount do
  54.             if ((merge[j,1] = buf) and (merge[j,2] = t1.rooms[i]) or
  55.                 (merge[j,1] = t1.rooms[i]) and (merge[j,2] = buf)) then begin
  56.                   f := 1;
  57.                   break;
  58.                 end;
  59.           if f = 1 then continue;
  60.           for j := i to Length(t2.s) do
  61.             if t2.rooms[j] = buf then
  62.               t2.rooms[j] := t1.rooms[i];
  63.           inc(MergeCount);
  64.           SetLength(merge, Length(merge) + 1);
  65.           merge[MergeCount,1] := buf;
  66.           merge[MergeCount,2] := t1.rooms[i];
  67.           dec(ans);
  68.           continue;
  69.         end;
  70.         if t1.rooms[i] = t2.rooms[i] then continue;
  71.         t1.rooms[i] := t2.rooms[i];
  72.         if (i > 1) and (t1.rooms[i - 1] = 0) and (t1.s[i - 1] = ' ') then begin
  73.           j := i;
  74.           while (j > 0) and (t1.s[j] = ' ') do begin
  75.             t1.rooms[j] := t2.rooms[i];
  76.             dec(j);
  77.           end;
  78.         end;
  79.         j := i;
  80.         f := 0;
  81.         buf := t2.rooms[i];
  82.         while (j <= Length(t1.s)) and (t1.s[j] = ' ') do begin
  83.           t1.rooms[j] := buf;
  84.           inc(j);
  85.           if (buf <> t2.rooms[j]) and (t2.rooms[j] <> 0) then f := 1;
  86.           if f = 0 then inc(i);
  87.         end;
  88.       end;
  89.     end;
  90.  
  91.     i := 0;
  92.     while i <= Length(t1.s) do begin
  93.       inc(i);
  94.       if (t1.s[i] = ' ') and (t1.rooms[i] = 0) then begin
  95.         inc(ans);
  96.         inc(k);
  97.         t1.rooms[i] := k;
  98.         while (i <= Length(t1.s)) and (t1.s[i] = ' ') do begin
  99.           t1.rooms[i] := k;
  100.           inc(i);
  101.         end;
  102.       end;
  103.     end;
  104.  
  105.   end;
  106.   write(ans);
  107. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement