讨论 / 虫食算为什么超时
dislike 2009-06-13 02:15:00
点我顶贴 收藏 删除
var

hash:array[’A’..’Z’] of boolean;

hash2:array[0..25] of boolean;

a:array[’A’..’Z’] of longint;

o,q,p:array[1..26] of longint;

n,i,j,k,l:longint;

x,sa,sb,sc:string;

ok:boolean;

procedure init(c:char);

begin

hash[c]:=false;

x:=x+c;

end;

procedure print;

var i:longint;

begin

for i:=1 to n-1 do

write(a[chr(i+64)],’ ’);

writeln(a[chr(n+64)]);

ok:=true;

end;

function good:boolean;

var

i,j:longint;

begin

j:=0;

for i:=n downto 1 do

begin

if (a[sa[i]]+a[sb[i]]+j) mod n <> a[sc[i]] then exit(false);

j:=(a[sa[i]]+a[sb[i]]+j) div n;

end;

if j>0 then exit(false);

exit(true);

end;

procedure treatment;

var i,j:longint;

begin

for i:=1 to 3 do

for j:=1 to n do

case i of

1:o[j]:=a[sa[j]];

2:p[j]:=a[sb[j]];

3:q[j]:=a[sc[j]];

end;

end;

function check:boolean;

var

i,j,k:longint;

begin

for i:=n downto 1 do

begin

if (a[sa[i]]=0) and (a[sb[i]]>0) and (a[sc[i]]>0) and hash2[0] then

begin

j:=(a[sc[i]]-a[sb[i]]+n) mod n;

k:=(a[sc[i]]-a[sb[i]]-1+n) mod n;

if not hash2[j] and not hash2[k] then exit(false);

end;

if (a[sa[i]]>0) and (a[sb[i]]=0) and (a[sc[i]]>0) and hash2[0] then

begin

j:=(a[sc[i]]-a[sa[i]]+n) mod n;

k:=(a[sc[i]]-a[sa[i]]-1+n) mod n;

if not hash2[j] and not hash2[k] then exit(false);

end;

if (a[sa[i]]>0) and (a[sb[i]]>0) and (a[sc[i]]=0) and hash2[0] then

begin

j:=(a[sa[i]]+a[sb[i]]) mod n;

k:=(a[sa[i]]+a[sb[i]]+1) mod n;

if not hash2[j] and not hash2[k] then exit(false);

end;

if (a[sa[i]]>0) and (a[sb[i]]>0) and (a[sc[i]]>0) then

begin

j:=(a[sa[i]]+a[sb[i]]) mod n;

k:=(a[sa[i]]+a[sb[i]]+1) mod n;

if (j<>a[sc[i]]) and (k<>a[sc[i]]) then exit(false);

end;

end;

exit(true);

end;

procedure search(s:longint);

var

i,j,k,l:longint;

begin

if ok then exit;

if (s>n) and good then print;

if not check then exit;

for i:=0 to n-1 do

if hash2[i] then

begin

hash2[i]:=false; a[x[s]]:=i;

search(s+1);

hash2[i]:=true; a[x[s]]:=0;

end;

end;

begin

assign(input,’alpha.in’);reset(input);

assign(output,’alpha.ans’);rewrite(output);

readln(n);

readln(sa);readln(sb);readln(sc);

x:=’’;

fillchar(hash,sizeof(hash),true);

fillchar(hash2,sizeof(hash2),true);

for i:=n downto 1 do

begin

if hash[sa[i]] then init(sa[i]);

if hash[sb[i]] then init(sb[i]);

if hash[sc[i]] then init(sc[i]);

end;

ok:=false;

search(1);

close(input);close(output);

end.

查看更多回复
提交回复