您的位置:首页 > 其它

noip2004-虫食算 2008.11.5

2016-02-05 13:25 295 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: