Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- uses Math, sysutils;
- {$mode objfpc}{$H+}
- type
- myType = record
- s: string;
- rooms: array [1..5000] of integer;
- end;
- var
- t1, t2: myType;
- i, j, ans, buf, k, MergeCount, f: integer;
- merge: array of array[1..2] of integer;
- begin
- assign(input,'input.txt');
- assign(output,'output.txt');
- reset(input);
- rewrite(output);
- readln(t1.s);
- ans := 0;
- k := 0;
- i := 0;
- MergeCount := 0;
- SetLength(merge, 1);
- while i <= Length(t1.s) do begin
- inc(i);
- if (t1.s[i] = ' ') then begin
- inc(ans);
- inc(k);
- t1.rooms[i] := k;
- while (i <= Length(t1.s)) and (t1.s[i] = ' ') do begin
- t1.rooms[i] := k;
- inc(i);
- end;
- end;
- end;
- while not eof(input) do begin
- t2.s := t1.s;
- for i := 1 to Length(t1.s) do begin
- t2.rooms[i] := t1.rooms[i];
- t1.rooms[i] := 0;
- end;
- readln(t1.s);
- i := 0;
- while i <= Length(t2.s) do begin
- inc(i);
- if (t2.s[i] = ' ') and (t1.s[i] = ' ') then begin
- if (t1.rooms[i] <> t2.rooms[i]) and (t1.rooms[i] <> 0) then begin
- buf := t2.rooms[i];
- f := 0;
- for j := 1 to MergeCount do
- if ((merge[j,1] = buf) and (merge[j,2] = t1.rooms[i]) or
- (merge[j,1] = t1.rooms[i]) and (merge[j,2] = buf)) then begin
- f := 1;
- break;
- end;
- if f = 1 then continue;
- for j := i to Length(t2.s) do
- if t2.rooms[j] = buf then
- t2.rooms[j] := t1.rooms[i];
- inc(MergeCount);
- SetLength(merge, Length(merge) + 1);
- merge[MergeCount,1] := buf;
- merge[MergeCount,2] := t1.rooms[i];
- dec(ans);
- continue;
- end;
- if t1.rooms[i] = t2.rooms[i] then continue;
- t1.rooms[i] := t2.rooms[i];
- if (i > 1) and (t1.rooms[i - 1] = 0) and (t1.s[i - 1] = ' ') then begin
- j := i;
- while (j > 0) and (t1.s[j] = ' ') do begin
- t1.rooms[j] := t2.rooms[i];
- dec(j);
- end;
- end;
- j := i;
- f := 0;
- buf := t2.rooms[i];
- while (j <= Length(t1.s)) and (t1.s[j] = ' ') do begin
- t1.rooms[j] := buf;
- inc(j);
- if (buf <> t2.rooms[j]) and (t2.rooms[j] <> 0) then f := 1;
- if f = 0 then inc(i);
- end;
- end;
- end;
- i := 0;
- while i <= Length(t1.s) do begin
- inc(i);
- if (t1.s[i] = ' ') and (t1.rooms[i] = 0) then begin
- inc(ans);
- inc(k);
- t1.rooms[i] := k;
- while (i <= Length(t1.s)) and (t1.s[i] = ' ') do begin
- t1.rooms[i] := k;
- inc(i);
- end;
- end;
- end;
- end;
- write(ans);
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement