讨论 / 巨丑pascal代码,但效率还算可以
wanlun11 2011-10-25 04:09:00
点我顶贴 收藏 删除
有效耗时: 656毫秒

测试结果1: 通过本测试点|有效耗时78ms

测试结果2: 通过本测试点|有效耗时62ms

测试结果3: 通过本测试点|有效耗时63ms

测试结果4: 通过本测试点|有效耗时62ms

测试结果5: 通过本测试点|有效耗时63ms

测试结果6: 通过本测试点|有效耗时62ms

测试结果7: 通过本测试点|有效耗时63ms

测试结果8: 通过本测试点|有效耗时78ms

测试结果9: 通过本测试点|有效耗时62ms

测试结果10:通过本测试点|有效耗时63ms

const nam='alpha.';

var a,b,c:array [1..26] of char;

v:array [1..26] of boolean;

f:array [1..26] of char;

t:array ['A'..'Z'] of boolean;

g:array ['A'..'Z'] of longint;

n:longint;

procedure init;

var i,j:longint;

s:array [1..3] of string;

ch:char;

begin

assign(input,nam+'in'); reset(input);

readln(n);

for i:=1 to 3 do

begin

readln(s[i]);

for j:=1 to (n div 2) do

begin

ch:=s[i][j];

s[i][j]:=s[i][n+1-j];

s[i][n+1-j]:=ch;

end;

end;

close(input);

for i:=1 to n do a[i]:=s[1][i];

for i:=1 to n do b[i]:=s[2][i];

for i:=1 to n do c[i]:=s[3][i];

fillchar(v,sizeof(v),0);

fillchar(t,sizeof(t),0);

end;

procedure print;

var ch:char;

begin

assign(output,nam+'out'); rewrite(output);

for ch:='A' to chr(ord('A')+n-1) do write(g[ch],' ');

close(output);

halt;

end;

function right(k:longint):boolean;

var i,j:longint;

begin

for i:=k to n do

begin

if t[a[i]] and t[b[i]] and t[c[i]] then

if ((g[a[i]]+g[b[i]]) mod n<>g[c[i]]) and ((g[a[i]]+g[b[i]]+1) mod n<>g[c[i]])

then exit(false) else continue;

if t[a[i]] and t[b[i]] then

if (v[(g[a[i]]+g[b[i]]) mod n]=true) and (v[(g[a[i]]+g[b[i]]+1) mod n]=true)

then exit(false) else continue;

if t[a[i]] and t[c[i]] then

if (v[(g[c[i]]+n-g[a[i]]) mod n]=true) and (v[(g[c[i]]+n-g[a[i]]-1) mod n]=true)

then exit(false) else continue;

if t[b[i]] and t[c[i]] then

if (v[(g[c[i]]+n-g[b[i]]) mod n]=true) and (v[(g[c[i]]+n-g[b[i]]-1) mod n]=true)

then exit(false) else continue;

end;

exit(true);

end;

procedure work(k,p:longint);

var i,j:longint;

x1,x2,x3:longint;

begin

if k=n+1 then print;

if t[a[k]]

then begin

x1:=g[a[k]];

if t[b[k]]

then begin

x2:=g[b[k]];

if t[c[k]]

then begin

x3:=g[c[k]];

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end

else begin

for x3:=n-1 downto 0 do

if v[x3]=false then

begin

v[x3]:=true;

f[x3]:=c[k];

t[c[k]]:=true;

g[c[k]]:=x3;

if right(k) then

begin

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end;

v[x3]:=false;

t[c[k]]:=false;

end;

end;

end

else begin

for x2:=n-1 downto 0 do

if v[x2]=false then

begin

v[x2]:=true;

f[x2]:=b[k];

t[b[k]]:=true;

g[b[k]]:=x2;

if right(k) then

begin

if t[c[k]]

then begin

x3:=g[c[k]];

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end

else begin

for x3:=n-1 downto 0 do

if v[x3]=false then

begin

v[x3]:=true;

f[x3]:=c[k];

t[c[k]]:=true;

g[c[k]]:=x3;

if right(k) then

begin

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end;

v[x3]:=false;

t[c[k]]:=false;

end;

end;

end;

v[x2]:=false;

t[b[k]]:=false;

end;

end;

end

else begin

for x1:=n-1 downto 0 do

if v[x1]=false then

begin

v[x1]:=true;

f[x1]:=a[k];

t[a[k]]:=true;

g[a[k]]:=x1;

if right(k) then

begin

if t[b[k]]

then begin

x2:=g[b[k]];

if t[c[k]]

then begin

x3:=g[c[k]];

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end

else begin

for x3:=n-1 downto 0 do

if v[x3]=false then

begin

v[x3]:=true;

f[x3]:=c[k];

t[c[k]]:=true;

g[c[k]]:=x3;

if right(k) then

begin

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end;

v[x3]:=false;

t[c[k]]:=false;

end;

end;

end

else begin

for x2:=n-1 downto 0 do

if v[x2]=false then

begin

v[x2]:=true;

f[x2]:=b[k];

t[b[k]]:=true;

g[b[k]]:=x2;

if right(k) then

begin

if t[c[k]]

then begin

x3:=g[c[k]];

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end

else begin

for x3:=n-1 downto 0 do

if v[x3]=false then

begin

v[x3]:=true;

f[x3]:=c[k];

t[c[k]]:=true;

g[c[k]]:=x3;

if right(k) then

begin

if x1+x2+p=x3 then work(k+1,0);

if x1+x2+p=x3+n then work(k+1,1);

end;

v[x3]:=false;

t[c[k]]:=false;

end;

end;

end;

v[x2]:=false;

t[b[k]]:=false;

end;

end;

end;

v[x1]:=false;

t[a[k]]:=false;

end;

end;

end;

Begin

init;

work(1,0);

End.

9kb啊9kb~~~

查看更多回复
提交回复