记录编号 2818 评测结果 AWWWWWWTWW
题目名称 [NOIP 2004]虫食算 最终得分 10
用户昵称 Gravatar苏轼 是否通过 未通过
代码语言 Pascal 运行时间 1.237 s
提交时间 2008-09-27 22:50:02 内存使用 0.51 MiB
显示代码纯文本
program cch(input,output);
var
 a:array[1..3,1..26] of char;
 b:array[1..26] of boolean;
 c:array[1..26] of integer;
 f:array[0..25] of boolean;
 d:array[1..26] of integer;
 i,j,n,tot:integer;

procedure search(k,p,now:integer);
var
 i,j,p1:integer;
 flag:boolean;
begin
 if k=n+1 then
  begin
    for j:=1 to n-1 do write(d[j],' ');
    write(d[n]);
    close(input); close(output);
    halt;
  end;
 for i:=0 to n-1 do
  if f[i] then
   begin
    d[c[k]]:=i; f[i]:=false;
    flag:=true;
    for j:=1 to 3 do
     if d[ord(a[j,now])-64]=-1 then flag:=false;
    if flag then
     begin
      if (d[ord(a[1,now])-64]+d[ord(a[2,now])-64]+p) mod n=d[ord(a[3,now])-64] then
       begin
        p1:=(d[ord(a[1,now])-64]+d[ord(a[2,now])-64]+p) div n;
        if (now<n)or(now=n)and(p1=0) then
         begin
          search(k+1,p1,now+1);
          f[i]:=true; d[c[k]]:=-1;
         end;
       end
      else begin f[i]:=true;  d[c[k]]:=-1; end;
     end
    else begin
       search(k+1,p,now);
       f[i]:=true; d[c[k]]:=-1;
      end;
  end;
end;

begin
 assign(input,'alpha.in');
 assign(output,'alpha.out');
 reset(input);
 rewrite(output);
 readln(n);
 for i:=1 to 3 do
  begin
   for j:=n downto 1 do
    read(a[i,j]);
   readln;
  end;
 for i:=1 to n do b[i]:=true;
 tot:=0;
 for j:=1 to n do
  for i:=1 to 3 do
   if b[ord(a[i,j])-64] then
    begin
     b[ord(a[i,j])-64]:=false;
     inc(tot);
     c[tot]:=ord(a[i,j])-64;
    end;
 for i:=0 to n-1 do f[i]:=true;
 for i:=1 to n do d[i]:=-1;
 search(1,0,1);
end.