比赛 20120706 评测结果 AAATTTTTTT
题目名称 解密 最终得分 30
用户昵称 czp 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2012-07-06 10:02:35
显示代码纯文本
type pa=^pb;
pb=record
 s1:ansistring;
 dd:longint;
 n:pa;
 end;
var
 a,b,next1,f,next2:array[0..1000000] of longint;
 hash:array[0..137730] of pa;
 i,j,m,n,tt,n1,n2,sum1:longint;
 ch:char;
 s:string;
 function insert(s:string):longint;
 var date:int64;o:boolean;p,pv:pa;i:longint;
 begin
  date:=1;
  for i:=1 to length(s) do
   date:=(date mod 137731)*(date mod 137731)*ord(s[i]) mod 137731;
  p:=hash[date];
  while p<>nil do
   begin
    if s=p^.s1 then break;
    p:=p^.n;
   end;
  if p<>nil then insert:=p^.dd else
   begin
    inc(tt);
    new(pv);
    pv^.s1:=s;
    pv^.dd:=tt;
    pv^.n:=hash[date];
    hash[date]:=pv;
    insert:=tt;
   end;
 end;
function pan(x:longint):boolean;
var i,j,t:longint;
begin
 pan:=true;
 for j:=1 to 10 do
   begin
     i:=random(n2);
  begin
   t:=next1[i+x];
   if i+1+next1[i+x]>n2 then t:=0;
   if t<>next2[i+1] then begin pan:=false; break; end;
  end;
   end;
 for i:=0 to n2-1 do
  begin
   t:=next1[i+x];
   if i+1+next1[i+x]>n2 then t:=0;
   if t<>next2[i+1] then begin pan:=false; break; end;
  end;
end;
begin
 assign(input,'kriptogram.in');reset(input);
 assign(output,'kriptogram.out');rewrite(output);
 randomize;
 while not eoln do
  begin
   inc(i);
   s:='';
   repeat
    read(ch);
    if ch='$' then break;
    s:=s+ch;
   until ch=' ';
  if ch='$' then break;
  a[i]:=insert(s);
  end;
 readln;
 n1:=i-1;tt:=0;
 for i:=0 to 137730 do  hash[i]:=nil;
 i:=0;
 while not eoln do
  begin
   inc(i);
   s:='';
   repeat
    read(ch);
    if ch='$' then break;
    s:=s+ch;
   until ch=' ';
  if ch='$' then break;
  b[i]:=insert(s);
  end;
 n2:=i-1;
 for i:=1 to n1 do
  begin
   next1[f[a[i]]]:=i-f[a[i]];
   f[a[i]]:=i;
  end;
 fillchar(f,sizeof(f),0);
 for i:=1 to n2 do
  begin
   next2[f[b[i]]]:=i-f[b[i]];
   f[b[i]]:=i;
  end;
 f[0]:=0;
 for  i:=1 to n1-n2+1 do
    if pan(i) then begin writeln(i);  break; end;
 close(input);close(output);
 end.