字典树
它是一个快速插入和查询的多叉树 支持字符串的操作 支持查询和插入
使用边来标记我的字母 它的路径代表着这个字符串 比如1 2 6 11这条路径的意思就是字符串aba 如果要插入字符就在应该有的字符串路径插入应该要的字符
Code:
#include <bits/stdc++.h>using namespace std; #define debug(x) cerr << #x << ": " << x << '\n'; typedef long long i64; template <typename T> struct Trie {int m;int N = 2e6;int now, tot;vector<T> val;vector<int> sz;vector<map<T, int> > nxt;Trie():val(N), sz(1, 0), tot(1), nxt(1, map<T, int>()){}// char & stringvoid add(string s) {now = 0;for (int i = 0; i < s.size(); i++){ if (nxt[now][s[i]] == 0) {nxt[now][s[i]] = tot; sz.push_back(0); val[tot++] = s[i];}now = nxt[now][s[i]]; sz[now]++;nxt.push_back(map<T, int>());}}int query(string t){now = 0;for (int i = 0; i < t.size(); i++){if (nxt[now][t[i]] == 0) return 0;now = nxt[now][t[i]];}return sz[now];} };int main() {ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);Trie<char> trie; return 0; }