【并查集】破译密文

“`ruby
var
p:array [‘a’..’z’]of record b,e,l:longint; c:boolean; end;
f,a1,a2:array [-2..10000] of longint;
s1,s2:ansistring;c:char;
n,i,l,j,x,y,ans,z,temp:longint;
function father(x:longint):longint; begin
if (f[x]=x)or(f[x]=0) then exit(f[x]) else
begin
f[x]:=father(f[x]);
father:=f[x];
end;
end;
procedure ex; begin
write(0); close(input);close(output);halt;
end;
begin
assign(input,’encrypt.in’);reset(input);
assign(output,’encrypt.out’);rewrite(output);
readln(s1);readln(s2);
readln(n);
for i:=1 to n do readln(c,p[c].l);
for i:=1 to length(s1) do if (s1[i]<>’1’)and(s1[i]<>’0’) then p[s1[i]].c:=true;
for i:=1 to length(s2) do if (s2[i]<>’1’)and(s2[i]<>’0’) then p[s2[i]].c:=true;
l:=2;
for c:=’a’ to ‘z’ do if p[c].c then
begin
p[c].b:=l;
inc(l,p[c].l);
p[c].e:=l-1;
//writeln(c,’ ‘,p[c].b,’ ‘,p[c].e);
end;
z:=l-1;
for i:=1 to l do f[i]:=i;
l:=0;
for i:=1 to length(s1) do
case s1[i] of
‘1’:begin inc(l);a1[l]:=1;end;
‘0’:begin inc(l);a1[l]:=0;end;
else
begin
for j:=p[s1[i]].b to p[s1[i]].e do
begin
inc(l);
f[j]:=j;
a1[l]:=j;
end;
end;end;
l:=0;
for i:=1 to length(s2) do
case s2[i] of
‘1’:begin inc(l);a2[l]:=1;if a1[l]=0 then ex; end;
‘0’:begin inc(l);a2[l]:=0;if a1[l]=1 then ex; end;
else
begin
for j:=p[s2[i]].b to p[s2[i]].e do
begin
inc(l);
f[j]:=j;
a2[l]:=j;
end;
end;end;
//for i:=1 to l do writeln(a1[i],’ ‘,a2[i]);
//for i:=1 to 10 do writeln(‘f ‘,f[i]);
for i:=1 to l do
begin
x:=father(a1[i]);y:=father(a2[i]);
if x+y=1 then ex;
if x>y then f[x]:=y else f[y]:=x;
// writeln(a1[i],’ ‘,a2[i],’ ‘,x,’ ‘,y,’ ‘,f[x],’ ‘,f[y]);
end;
ans:=0;f[1]:=1;
for i:=2 to z do
begin
temp:=father(f[i]);//writeln(temp,’ ‘,i);
if (temp<>1)and(temp<>0) then begin inc(ans); f[temp]:=0;end;
end;
writeln(1 shl ans);
close(input);close(output);
end.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值