| 1 | { Search.raz's twin. } |
| 2 | program search; |
| 3 | {$mode objfpc} |
| 4 | const |
| 5 | N = 100000; |
| 6 | Rounds = 400; |
| 7 | var |
| 8 | text: array[0..N - 1] of byte; |
| 9 | word: array[0..15] of byte; |
| 10 | i, r, x, total: int64; |
| 11 | |
| 12 | function Find(const s, sub: array of byte): int64; |
| 13 | var i, j: int64; |
| 14 | begin |
| 15 | for i := 0 to Length(s) - Length(sub) do begin |
| 16 | j := 0; |
| 17 | while (j < Length(sub)) and (s[i + j] = sub[j]) do inc(j); |
| 18 | if j = Length(sub) then exit(i); |
| 19 | end; |
| 20 | Find := -1; |
| 21 | end; |
| 22 | |
| 23 | begin |
| 24 | x := 1; |
| 25 | for i := 0 to N - 1 do begin |
| 26 | x := (x * 1103515245 + 12345) and $7FFFFFFF; |
| 27 | text[i] := ord('a') + (x shr 16) and 3; |
| 28 | end; |
| 29 | total := 0; |
| 30 | for r := 1 to Rounds do begin |
| 31 | Move(text[N - 16 - r], word, 16); |
| 32 | total := total + Find(text, word); |
| 33 | end; |
| 34 | writeln(total); |
| 35 | end. |