| 1 | { Grep.raz's twin. } |
| 2 | program grep; |
| 3 | {$mode objfpc} |
| 4 | const |
| 5 | N = 16777216; |
| 6 | Rounds = 16; |
| 7 | Letters: string[16] = 'etaoinshrdlucmfw'; |
| 8 | Ones = qword($0101010101010101); |
| 9 | Tops = qword($8080808080808080); |
| 10 | Word: string[5] = 'error'; |
| 11 | A = 1; |
| 12 | B = 3; |
| 13 | var |
| 14 | text: array[0..N - 1] of byte; |
| 15 | i, r, x, v, total, hits: int64; |
| 16 | |
| 17 | function Zeros(v: qword): qword; |
| 18 | begin |
| 19 | Zeros := (v - Ones) and not v and Tops; |
| 20 | end; |
| 21 | |
| 22 | function Lowest(m: qword): int64; |
| 23 | begin |
| 24 | Lowest := int64((((m and (0 - m)) - 1) and Ones) * Ones shr 56) - 1; |
| 25 | end; |
| 26 | |
| 27 | function Holds(s: PByte): boolean; |
| 28 | var k: int64; |
| 29 | begin |
| 30 | for k := 0 to Length(Word) - 1 do |
| 31 | if s[k] <> ord(Word[k + 1]) then exit(false); |
| 32 | Holds := true; |
| 33 | end; |
| 34 | |
| 35 | function LineEnd(s: PByte; n: int64): int64; |
| 36 | var i: int64; m: qword; |
| 37 | begin |
| 38 | i := 0; |
| 39 | while i + 8 <= n do begin |
| 40 | m := Zeros(PQWord(@s[i])^ xor qword($0A0A0A0A0A0A0A0A)); |
| 41 | if m <> 0 then exit(i + Lowest(m)); |
| 42 | i := i + 8; |
| 43 | end; |
| 44 | while (i < n) and (s[i] <> 10) do inc(i); |
| 45 | LineEnd := i; |
| 46 | end; |
| 47 | |
| 48 | function Find(s: PByte; n: int64): int64; |
| 49 | var i, j, last: int64; m, wa, wb: qword; |
| 50 | begin |
| 51 | last := n - Length(Word); |
| 52 | wa := qword(ord(Word[A + 1])) * Ones; |
| 53 | wb := qword(ord(Word[B + 1])) * Ones; |
| 54 | i := 0; |
| 55 | while i + 7 <= last do begin |
| 56 | m := Zeros(PQWord(@s[i + A])^ xor wa) and Zeros(PQWord(@s[i + B])^ xor wb); |
| 57 | while m <> 0 do begin |
| 58 | j := i + Lowest(m); |
| 59 | if Holds(@s[j]) then exit(j); |
| 60 | m := m and (m - 1); |
| 61 | end; |
| 62 | i := i + 8; |
| 63 | end; |
| 64 | while i <= last do begin |
| 65 | if Holds(@s[i]) then exit(i); |
| 66 | inc(i); |
| 67 | end; |
| 68 | Find := -1; |
| 69 | end; |
| 70 | |
| 71 | function Lines(s: PByte; len: int64): int64; |
| 72 | var i, n: int64; |
| 73 | begin |
| 74 | i := 0; |
| 75 | n := 0; |
| 76 | while i < len do begin |
| 77 | i := i + LineEnd(@s[i], len - i) + 1; |
| 78 | inc(n); |
| 79 | end; |
| 80 | Lines := n; |
| 81 | end; |
| 82 | |
| 83 | function Matching(s: PByte; len: int64): int64; |
| 84 | var i, k, n: int64; |
| 85 | begin |
| 86 | i := 0; |
| 87 | n := 0; |
| 88 | k := Find(s, len); |
| 89 | while k >= 0 do begin |
| 90 | inc(n); |
| 91 | i := i + k; |
| 92 | i := i + LineEnd(@s[i], len - i) + 1; |
| 93 | if i >= len then exit(n); |
| 94 | k := Find(@s[i], len - i); |
| 95 | end; |
| 96 | Matching := n; |
| 97 | end; |
| 98 | |
| 99 | begin |
| 100 | x := 1; |
| 101 | for i := 0 to N - 1 do begin |
| 102 | x := (x * 1103515245 + 12345) and $7FFFFFFF; |
| 103 | v := (x shr 16) and 63; |
| 104 | if v = 0 then text[i] := 10 |
| 105 | else if v < 10 then text[i] := ord(' ') |
| 106 | else text[i] := ord(Letters[(v and 15) + 1]); |
| 107 | end; |
| 108 | text[N - 1] := 10; |
| 109 | i := 0; |
| 110 | while i < N - 1000 do begin |
| 111 | Move(Word[1], text[i], Length(Word)); |
| 112 | i := i + 997; |
| 113 | end; |
| 114 | total := 0; |
| 115 | hits := 0; |
| 116 | for r := 1 to Rounds do begin |
| 117 | total := total + Lines(@text[0], N); |
| 118 | hits := hits + Matching(@text[0], N); |
| 119 | end; |
| 120 | writeln(total, ' ', hits); |
| 121 | end. |