noip2004-虫食算 2008.11.5

noip2004-虫食算 2008.11.5

Program 1:辛苦了半天敲上去的程序,结果发现,忘了考虑一列中有相同的字母的情况一定要想到没有下次再开始敲

program alpha;{分了好多种情况}
const fin='alpha.in';fout='lapha.out';
var f1,f2:text;
    n,i,j,nn:longint;
    c:array['A'..'Z']of longint;
    b:array[0..26]of boolean;
    x1,x2,x:string;
procedure init;
var i:longint;ch:char;
begin
  fillchar(b,sizeof(b),true);
  assign(f1,fin);reset(f1);
  assign(f2,fout);rewrite(f2);
  readln(f1,n);
  readln(f1,x1);
  readln(f1,x2);
  readln(f1,x);
  for ch:='A' to 'Z' do
    c[ch]:=-1;
  nn:=n+1;
end;
procedure print;
var ch:char;
begin
  for ch:='A' to 'Z' do
    write(f2,c[ch]);
  close(f1);close(f2);halt;
end;
procedure deal(p,y:longint);
var i,r,t1,t2,k1,k2,k3,k4,k5,k6:longint;s1,s2,s3:boolean;
begin
  if p=n+1 then begin print;exit;end;
  r:=nn-p;
  if c[x1[r]]=-1 then s1:=false else s1:=true;
  if c[x2[r]]=-1 then s2:=false else s2:=true;
  if c[x[r]]=-1 then s3:=false else s3:=true;
  if (s1)and(s2)and(s3) and((c[x1[r]]+c[x2[r]]+y)mod n=c[x[r]]) then
                     deal(p+1,(c[x1[r]]+c[x2[r]]+y)div n);

  if (s1) and (s2) and (not(s3)) then
      begin
        t1:=c[x1[r]]+c[x2[r]];
        t2:=t1 mod n;
        if b[t2] then
          begin c[x[r]]:=t2;y:=t1 div n;
                b[t2]:=false;
                deal(p+1,y);
                b[t2]:=true;
                c[x[r]]:=-1;
          end
      end;

  if (s1) and (not(s2)) and (s3) then
    begin
      t1:=c[x[r]]-c[x1[r]];
      if t1<0 then begin t1:=t1+n;y:=1;end else y:=0;
      if b[t1] then
        begin
          c[x2[r]]:=t1;
          b[t1]:=false;
          deal(p+1,y);
          c[x2[r]]:=-1;
          b[t1]:=true;
        end
    end;
 if (not(s1))and (s2) and (s3) then
    begin
      t1:=c[x[r]]-c[x2[r]];
      if t1<0 then begin t1:=t1+n;y:=1;end else y:=0;
      if b[t1] then
        begin
          c[x1[r]]:=t1;
          b[t1]:=false;
          deal(p+1,y);
          c[x1[r]]:=-1;
          b[t1]:=true;
        end
    end;
  if (s1) and (not(s2))and (not(s3)) then
      for k1:=0 to n do
        if b[k1] then
          begin
            b[k1]:=false;
            t1:=c[x1[r]]+k1;
            t2:=t1 mod n;
            if b[t2] then
              begin
                c[x2[r]]:=k1;
                c[x[r]]:=t2;
                y:=t1 div n;
                b[t2]:=false;
                deal(p+1,y);
                b[t2]:=true;
                c[x[r]]:=-1;
                c[x2[r]]:=-1;
              end;
            b[k1]:=true;
          end;
  if (not(s1))and (s2) and (not(s3)) then
     for k3:=0 to n do
       if b[k3] then
         begin
           b[k3]:=false;
           t1:=c[x2[r]]+k3;
           t2:=t1 mod n;
           if b[t2] then
             begin
               c[x1[r]]:=k3;
               c[x[r]]:=t2;
               y:=t1 div n;
               b[t2]:=false;
               deal(p+1,y);
               b[t2]:=true;
               c[x[r]]:=-1;
               c[x1[r]]:=-1;
             end;
           b[k3]:=true;
         end;
if (not(s1)) and (not(s2)) and (s3) then
    for k4:=0 to n do
      if b[k4] then
        begin
          b[k4]:=false;
          t1:=c[x[r]]-k4;
          if t1<0 then begin t1:=t1+n;y:=1;end else y:=0;
          if b[t1] then
            begin
              c[x1[r]]:=k4;
              c[x2[r]]:=t1;
              b[t1]:=false;
              deal(p+1,y);
              c[x1[r]]:=-1;
              c[x2[r]]:=-1;
              b[t1]:=true;
            end;
          b[k4]:=true;
        end;
if (not(s1))and (not(s1)) and (not(s3))
  then for k5:=0 to n do
       if b[k5] then
        begin
          b[k5]:=false;
          for k2:=0 to n do
            if b[k2] then
              begin
               b[k2]:=false;
               t1:=k5+k2;
               t2:=t1 mod n;
               if b[t2] then
                 begin
                   c[x1[r]]:=k5;
                   c[x2[r]]:=k2;
                   c[x[r]]:=t2;
                   y:=t1 div n;
                   b[t2]:=false;
                   deal(p+1,y);
                   c[x1[r]]:=-1;
                   c[x2[r]]:=-1;
                   c[x[r]]:=-1;
                   b[t2]:=true;
                 end;{if b[t2]}
               b[k2]:=true;
             end;{if b[k2]}
           b[k5]:=true;
          end;
end;{deal}
begin
  init;
  deal(1,0);
  close(f1);
  close(f2);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值