【模板】Trie

代码如下

int trie[maxn][26],tot=1,ed[maxn];

void insert(char *s){
	int len=strlen(s+1),now=1;
	for(int i=1;i<=len;i++){
		int ch=s[i]-'a';
		if(!trie[now][ch])trie[now][ch]=++tot;
		now=trie[now][ch];
	}
	ed[now]=1;
}

bool search(char *s){
	int len=strlen(s+1),now=1;
	for(int i=1;i<=len;i++){
		now=trie[now][s[i]-'a'];
		if(!now)return 0;
	}
	return ed[now];
}
posted @ 2018-10-27 21:17  shellpicker  阅读(108)  评论(0编辑  收藏  举报