LIGHTS - Lights

Tác giả: RR

Ngôn ngữ: Pascal

{$R+,Q+}
const
  FINP='';
  FOUT='';
  MAXN=10001;
var
  n,kq:longint;
  a,b:array[0..MAXN] of integer;
procedure inp;
var
  f:text;
  ch:char;
  i:longint;
begin
  assign(f,FINP); reset(f);
  readln(f,n);
  for i:=1 to n do
    begin
      read(f,ch);
      if ch='0' then a[i]:=0 else a[i]:=1;
    end;
  readln(f);
  for i:=1 to n do
    begin
      read(f,ch);
      if ch='0' then b[i]:=0 else b[i]:=1;
    end;
  close(f);
end;
procedure ans;
var
  f:text;
begin
  assign(f,FOUT); rewrite(f);
  writeln(f,kq);
  close(f);
end;
function check(a,b,c:byte):boolean;
begin
  check:=true;
  if (a=0) and (b=1) and (c=0) then exit;
  if (a=1) and (b=0) and (c=1) then exit;
  check:=false;
end;
procedure solve;
var
  i:longint;
begin
  kq:=0;
  for i:=1 to n do
    if check(a[i-1],a[i],a[i+1]) and check(b[i-1],b[i],b[i+1]) then
      begin
        a[i]:=1-a[i];
        b[i]:=1-b[i];
        inc(kq);
      end;
  for i:=1 to n do
    if (a[i-1]=0) and (a[i]=1) then inc(kq);
  for i:=1 to n do
    if (b[i-1]=0) and (b[i]=1) then inc(kq);
end;
begin
  inp;
  solve;
  ans;
end.

Download