比赛 20101117 评测结果 TTTTTTTTTA
题目名称 拯救 最终得分 10
用户昵称 wo shi 刘畅 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2010-11-17 11:08:34
显示代码纯文本
var
  n,t,p,i:longint;
  st,en:string;
  a:array[0..100000]of string;
  b:array[0..100000]of longint;

procedure print(k:longint);
begin
  writeln(k);
  close(input);
  close(output);
  halt;
end;

function yes:boolean;
var
  i:longint;
begin
  for i:=p-1 downto 1 do
  if a[i]=a[p] then exit(false);
  exit(true);
end;

procedure work(s:string);
var
  nn,i:longint;
begin
  inc(p);
  a[p]:=s;
  if a[p,1]='0' then a[p,1]:='1'
  else a[p,1]:='0';
  b[p]:=b[t]+1;
  if a[p]=en then print(b[p]);
  if not yes then dec(p);

  if s[1]='1' then
  begin
    inc(p);
    a[p]:=s;
    if a[p,2]='0' then a[p,2]:='1'
    else a[p,2]:='0';
    b[p]:=b[t]+1;
    if a[p]=en then print(b[p]);
    if not yes then dec(p);
  end;

  for i:=3 to n do
  if s[i-1]='1' then
  begin
    val(copy(s,1,i-2),nn);
    if nn=0 then
    begin
      inc(p);
      a[p]:=s;
      if a[p,i]='0' then a[p,i]:='1'
      else a[p,i]:='0';
      b[p]:=b[t]+1;
      if a[p]=en then print(b[p]);
      if not yes then dec(p);
    end;
  end;
end;

begin
  assign(input,'savey.in'); reset(input);
  assign(output,'savey.out'); rewrite(output);
  readln(n);
  readln(st);
  a[1]:='';
  for i:=1 to length(st) do
    if st[i] in ['0'..'1'] then a[1]:=a[1]+st[i];
  for i:=1 to n do en:=en+'0';
  t:=1;
  p:=1;
  repeat
    work(a[t]);
    inc(t);
  until t>p;
  close(input);
  close(output);
end.