String/Trie/Trie.h
Depends on
Code
#include "../../template.h"
struct Node {
Node *child[26];
bool isEnd;
Node() {
memset(child, 0, sizeof child);
isEnd = false;
}
};
class Trie {
Node *r = new Node();
Trie() {}
void add(const string &s) {
Node *u = r;
for (size_t i = 0; i < s.size(); i++) {
int k = s[i] - 'a';
if (!u->child[k]) {
u->child[k] = new Node();
}
u = u->child[k];
}
u->isEnd = true;
}
bool search(const string &s) {
Node *u = r;
for (size_t i = 0; i < s.size(); i++) {
int k = s[i] - 'a';
if (!u->child[k]) {
return false;
}
u = u->child[k];
}
return u->isEnd;
}
};
#line 2 "template.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD (ll)(1e9+7)
#define all(x) (x).begin(),(x).end()
#define unique(x) x.erase(unique(all(x)), x.end())
#define INF32 ((1ull<<31)-1)
#define INF64 ((1ull<<63)-1)
#define inf (ll)1e18
#define vi vector<int>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define fi first
#define se second
const int mod = 998244353;
void solve();
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);
// cin.exceptions(cin.failbit);
// int t; cin >> t;
// while(t--)
solve();
cerr << "\nTime run: " << 1000 * clock() / CLOCKS_PER_SEC << "ms" << '\n';
return 0;
}
#line 2 "String/Trie/Trie.h"
struct Node {
Node *child[26];
bool isEnd;
Node() {
memset(child, 0, sizeof child);
isEnd = false;
}
};
class Trie {
Node *r = new Node();
Trie() {}
void add(const string &s) {
Node *u = r;
for (size_t i = 0; i < s.size(); i++) {
int k = s[i] - 'a';
if (!u->child[k]) {
u->child[k] = new Node();
}
u = u->child[k];
}
u->isEnd = true;
}
bool search(const string &s) {
Node *u = r;
for (size_t i = 0; i < s.size(); i++) {
int k = s[i] - 'a';
if (!u->child[k]) {
return false;
}
u = u->child[k];
}
return u->isEnd;
}
};
Back to top page