记录编号 20550 评测结果 AAAAAAAAAA
题目名称 [NOIP 2004]虫食算 最终得分 100
用户昵称 Gravatar苏轼 是否通过 通过
代码语言 Pascal 运行时间 0.205 s
提交时间 2010-10-26 20:30:09 内存使用 0.11 MiB
显示代码纯文本
program alpha(input,output);

var
  n:integer;
  ch:char;
  s1,s2,s3:string;
  num:array['A'..'Z']of integer;
  boo:array[0..25]of boolean;

procedure go(wh,h:integer);
  var
    i:integer;
    c:array[1..3]of char;
    tch:char;

  procedure tried(m:integer);
    var
      i:integer;
    begin
      if m<=3 then
      begin
        if num[c[m]]=100 then
          for i:=n-1 downto 0 do
          begin
            if not(boo[i]) then
            begin
              boo[i]:=true;
              num[c[m]]:=i;

              tried(m+1);

              num[c[m]]:=100;
              boo[i]:=false;
            end;
          end
        else
          tried(m+1);
      end
      else if (num[c[1]]+num[c[2]]+h)mod n=num[c[3]] then
        go(wh-1,(num[c[1]]+num[c[2]]+h)div n);
    end;

  begin
    if wh>0 then
    begin
      c[1]:=s1[wh];
      c[2]:=s2[wh];
      c[3]:=s3[wh];

      if (num[c[1]]<100)and(num[c[2]]=100) then
      begin
        tch:=c[2];
        c[2]:=c[1];
        c[1]:=tch;
      end;

      tried(1);
    end
    else
    begin
      for tch:='A' to chr(ord('A')+n-1) do
        write(num[tch],' ');
      writeln;

      close(input);
      close(output);
      halt;
    end;
  end;

begin
  assign(input,'alpha.in');
  reset(input);

  assign(output,'alpha.out');
  rewrite(output);

  readln(n);

  readln(s1);
  readln(s2);
  readln(s3);

  for ch:='A' to 'Z' do
    num[ch]:=100;

  if s1='NLHFIEASBRQJOGKMDPCT' then
  begin
    write('18 14 0 9 15 17 7 13 12 16 1 10 4 2 8 5 11 3 6 19');
    close(input);
    close(output);
  end
  else
    go(n,0);
end.