(* A search: where a word first is in a text, by trying each place in turn, over a text of four letters. *) module Search; import Out; import SYSTEM; check off; const N = 100000; Rounds = 400; var text: [N]U8; (* where sub first starts in s, or -1 *) proc find(s, sub: []U8): INT; var i, j: INT; begin for i := 0 to LEN(s) - LEN(sub) do j := 0; while (j < LEN(sub)) and (s[i + j] = sub[j]) do INC(j); end; if j = LEN(sub) then return i; end; end; return -1; end; proc main*; var i, r, x, total: INT; word: [16]U8; begin x := 1; for i := 0 to N - 1 do x := (x * 1103515245 + 12345) & 7FFFFFFFH; text[i] := U8('a' + (x shr 16) & 3); end; total := 0; for r := 1 to Rounds do COPY(word, text[N - 16 - r..N - r]); total := total + find(text, word); end; Out.int(total); Out.ln; end; end Search.