俺的trie漂亮,這可不是吹的
網(wǎng)上許多trie施肥很足,比如C++版參數(shù)是string而不是const string&, Python版不用dict
class TrieNode: def __str__(this): return str((id(this) - id(root)) // 64)
def __init__(this): this.kids = {}; this.hasWord = False def insert(this, str): if str == '': this.hasWord = True; return kid = this.kids.setdefault(str[0], TrieNode()) kid.insert(str[1:]) def find(this, str): if str == '': return this.hasWord kid = this.kids.get(str[0]) return kid.find(str[1:]) if kid is not None else False def print(this, n=0): space = ' ' * n def pr(*args, **kwargs): print(space, *args, **kwargs, sep='') pr(f"I'm node {this}. These are my kids: ", end='') for ch,kid in this.kids.items(): print(f"'{ch}':{kid}", end=' ') print() for _,kid in this.kids.items(): kid.print(n + 1) root = TrieNode() root.insert('abc'); root.insert('abd'); root.insert('ab') r = root; print(r.find('abc'), r.find('ab'), r.find('a')) r.print()
True True False I'm node 0. These are my kids: 'a':12 I'm node 12. These are my kids: 'b':14 I'm node 14. These are my kids: 'c':15 'd':16 I'm node 15. These are my kids: I'm node 16. These are my kids:
也許space改叫indent好。 indent, from in- ‘into’ + 拉丁語(yǔ) dens, dent- ‘tooth’.
C++循環(huán),bit流:
#include <stdio.h> #include <stdint.h> #include <string.h> struct TrieNode; TrieNode* tnalloc(); class TrieNode { TrieNode* kids[2]; bool hasWord; int id; public: void insert (const void* buf, size_t n) { const uint8_t* bytes = (const uint8_t*)buf; TrieNode* p = this; while (n--) { for (uint8_t m = 0x80; m; m >>= 1) { const int i = bytes[n] & m ? 1 : 0; if (!p->kids[i]) p->kids[i] = tnalloc(); p = p->kids[i]; //printf("insert: %d\n", p->id); } } p->hasWord = true; //printf("%d has word\n", p->id); } bool find (const void* buf, size_t n) { const uint8_t* bytes = (const uint8_t*)buf; TrieNode* p = this; while (n--) { for (uint8_t m = 0x80; m; m >>= 1) { const int i = bytes[n] & m ? 1 : 0; if (!p->kids[i]) goto done; p = p->kids[i]; } } done: //printf("find: %d\n", p->id); return p->hasWord; } friend TrieNode* tnalloc(); }; TrieNode* tnalloc () { static TrieNode all[1000 * 1000]; static int n = 0; TrieNode* p = all + n++; p->kids[0] = p->kids[1] = 0; p->hasWord = false; p->id = n - 1; return p; } void insert(TrieNode* p, const char* s) { p->insert(s, strlen(s)); // 防"ab", 1 printf("%s in trie\n", s); } int main (int argc, char** argv) { if (argc != 2) return 0; TrieNode* r = tnalloc(); insert(r, "ab"); insert(r, "abc"); insert(r, "abd"); printf("%d\n", r->find(argv[1], strlen(argv[1]))); return 0; }
https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
各種算法的動(dòng)畫(huà),有trie的。再如八皇后的:
比如(人那個(gè)是會(huì)動(dòng)的哦):

〔.tar.gz 3.7M〕可下載到本地運(yùn)行。我搞了〔.7z 1.3M〕
樹(shù)和樹(shù)中的節(jié)點(diǎn)有時(shí)候不太好區(qū)分。指向數(shù)的指針是不是指向樹(shù)根的指針?樹(shù)根是不是個(gè)節(jié)點(diǎn)?
《讀者》還是某雜志說(shuō)外國(guó)有科學(xué)家研究結(jié)和環(huán),結(jié)論是“結(jié)是環(huán)的一個(gè)系統(tǒng)”。研究Knot is not naughty, 是正兒八經(jīng)的數(shù)學(xué)理論。
高斯想出了用數(shù)字串表示結(jié)的方法,但是同一個(gè)結(jié)可以有多種不同表示。
Perfect Hash函數(shù)不好找。有個(gè)老外把gpref的速度提高了一倍(也許不是所有情況),酸楚地抱怨沒(méi)人感興趣。

浙公網(wǎng)安備 33010602011771號(hào)