asm default false new static typeid auto delete float operator static_cast typename bool do for private struct union break double friend protected switch location case dynamic_cast goto public variable unsigned catch else if register template using char enum inline reinterpret_cast this virtual class explicit int return throw void const export long short true volatile const_cast declarations mutable signed try wchar_t continue extern namespace sizeof typedef while
An ordered list is at least ×10 smaller.
{ alb gel mug sog zip }
unsigned hash(word) return word[0]
'z' - 'a'
+ 1 = 26 in this case.
map['a'] = 0 map['g'] = 1 map['m'] = 2 map['s'] = 3 map['z'] = 4 map[*] = 5
unsigned hash(word) return map[word[0]]
bool
and break
are keys?
const
and const_cast
are keys?
unsigned hash(word) unsigned n = strlen(word) return n + map[word[0]] + map[word[n-1]]
map
as a 26-digit hexadecimal number.
map = 0 while map < 0xfffffffffffffffffffffffffff if not has-collisions(map, words) return map map++
map
has roughly 1626 possible values.
map
to a n-digit number, where n distinct
letters start or end the words.
map
fails, where's the problem?
solve(words, map, keys) if words is empty // success else pick a word in words for map[word[0]] = 0 to 15 for map[word[n-1]] = 0 to 15 if no-collisions(keys, map) solve(words - word, map) // failure
words
?
elk, egg, the, elf, end, pit
e: 5, k: 1, g: 1, t: 2, f: 1, d: 1, p: 1
f[word.first] + f[word.last]
egg, elf, ..., fog
egg, elf, fog, ...
g: 3, o: 2, e: 2, n: 2, w: 1
egg, gao, nog, ego, new
egg, gao, ego, nog, new
set 1 set 2 set 3 Unsorted 13.2 3.6 30.3 Sorted 0.2 0.0 0.0
set 4 set 5 set 6 Unsorted 0.6 2.1 9.7 Sorted 0.7 3.9 9.7
text: GAGCTCCTGCACTGGATGGTGGC pattern: CAAAGA
int match(text, pattern) t = p = 0 do if text[t] == pattern[p] t++; p++ else t = t - p + 1; p = 0 while t < T and p < P return p == P ? i - p : -1
- Don't move the pattern past the second occurence.
mismatch at p = 0 1 2 3 adjust t by 1 0 0 -1
if text[t] == pattern[p] t++; p++ else t += t-adj[p]; p = 0
A0T1G2A3B4A5T6G7how far should the pattern advance if there's a mismatch at A5?
A0 T1 G2 A3 B4 A5 T6 G7 A0 T1 G2 A3 B4 A5 T6 G7 A0 T1 G2 A3 B4 A5 T6 G7 A0 T1 G2 A3 B4 A5 T6 A0 T1 G2 A3 B4 A5 A0 T1 G2 A3 B4