字符串处理
字典树Trie
/* 字典树Trie */
struct Trie {
// 最大节点数:模式串个数 * 最大串长度
static const int MAX_NODE = 10000 * 50 + 50;
static const int CHAR_SET = 26; // 字符集的大小,也是Trie树中每个节点所连的最大边数
static const int BASE = 'a';
int n; // trie大小
int id[BASE+CHAR_SET+1]; // 字符->int
int tag[MAX_NODE]; // 标记,根据需要调整
int trie[MAX_NODE][CHAR_SET];
void init() {
for (int i=0;i<CHAR_SET;++i)
id[BASE+i] = i;
n = 1;
memset(trie[0], -1, sizeof(trie[0]));
memset(tag, 0, sizeof(tag));
}
// 添加一个单词
void add(const char *s) {
int p = 0;
while (*s) {
int i = id[*s];
if (trie[p][i] ==