Đề bài: http://yeulaptrinh.de/319/substr-spoj/
Thuật toán:
Code:
const fi=''; fo=''; maxn=trunc(1e6); base=trunc(1e9)+7; pp=307; var f : array[0..maxn] of int64; i,j,n,m : longint; hb : int64; a,b : array[1..maxn] of byte; ha,pow : array[0..maxn] of int64; procedure enter; var x : ansistring; begin assign(input,fi);reset(input); readln(x); m := length(x); for i := 1 to m do a[i] := ord(x[i])-48; readln(x); n := length(x); for i := 1 to n do b[i] := ord(x[i])-48; close(input); end; function gethash(l,r : longint) : int64; begin gethash := (ha[r]-ha[l-1]*pow[r-l+1] + base*base) mod base; end; procedure process; begin assign(output,fo);rewrite(output); ha[0] := 0; for i:=1 to m do ha[i] := (ha[i-1]*pp + a[i]) mod base; pow[0] := 1; for i:=1 to m do pow[i] := pow[i-1]*pp mod base; hb := 0; for i:=1 to n do hb := (hb*pp + b[i]) mod base; for i:=1 to m-n+1 do if hb = gethash(i,i+n-1) then write(i,' '); close(output); end; begin enter; process; end. |
Recent Comments