跳到正文

学习·文化课

字典树【模板】

shuyezy约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年2月18日
更新
2026年8月1日
篇幅
约 1 分钟

版本

  1. v1当前
C++
struct Trie {
	int next[N][26];
	int end[N], cnt;
	void insert(char * s, int l) {
		int p = 0;
		for(int i = 0; i < l; i++) {
			int c = s[i] - 'a';
			if(next[p][c] == 0) next[p][c] = ++cnt;
			p = next[p][c];
		}
		end[p]++;
	}
	bool find(char * s, int l) {
		int p = 0;
		for(int i = 0; i < l; i++) {
			int c = s[i] - 'a';
			if(!next[p][c]) return false;
			p = next[p][c];
		}
		return end[p];
	}
};
原文