Pagini recente » Cod sursa (job #90322) | Cod sursa (job #1362759) | Cod sursa (job #1107589) | Cod sursa (job #2862476) | Cod sursa (job #3340541)
/*
Author: Paul Ciumandru
Hard work beats talent!
*/
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#include <bits/stdc++.h>
using namespace std;
#define NMAX 100000
#define MMAX 50000
#define PMAX 524288
#define LOG 18
#define INF_INT 2e9
#define INF_LL 1e18
#define BS 666013
#define MOD 1000000007
#define debug(x) cout << #x << " = " << x << "\n";
#define vdebug(a) cout << #a << " = "; for(auto x: a) cout << x << " "; cout << "\n";
#define ll long long
#define ull unsigned long long
#define dbl double
#define ldb long double
#define pii pair<int, int>
#define pll pair<ll, ll>
#define piv pair<int, vector<int>>
#define pipii pair<int, pair<pii, pii>>
#define tpl tuple<int, int, int>
#define tpl2 tuple<int, int, vector<int>>
#define vpi vector<pii>
ifstream fin("trie.in");
ofstream fout("trie.out");
struct Trie_Node {
Trie_Node *nxt[26];
int cnt = 0, nr = 0;
Trie_Node() {
for (int i = 0; i < 26; i++) {
nxt[i] = NULL;
}
}
};
Trie_Node *rad = new Trie_Node();
int op;
string s;
void insert(Trie_Node *nod, const string &s) {
for (char ch : s) {
if (nod -> nxt[ch - 'a'] == NULL) {
nod -> nxt[ch - 'a'] = new Trie_Node();
}
nod = nod -> nxt[ch - 'a'];
nod -> nr++;
}
nod -> cnt++;
}
void erase(Trie_Node *nod, const string &s) {
for (char ch : s) {
nod = nod -> nxt[ch - 'a'];
nod -> nr--;
}
nod -> cnt--;
nod = rad;
for (char ch : s) {
if (nod -> nxt[ch - 'a'] -> nr == 0) {
nod -> nxt[ch - 'a'] = NULL;
return;
}
nod = nod -> nxt[ch - 'a'];
}
}
int count(Trie_Node *nod, const string &s) {
for (char ch : s) {
if (nod -> nxt[ch - 'a'] == NULL) {
return 0;
}
nod = nod -> nxt[ch - 'a'];
}
return nod -> cnt;
}
int lcp(Trie_Node *nod, const string &s) {
int lg = 0;
for (char ch : s) {
if (nod -> nxt[ch - 'a'] == NULL) {
return lg;
}
nod = nod -> nxt[ch - 'a'];
lg++;
}
return lg;
}
int main() {
ios_base::sync_with_stdio(false);
fin.tie(NULL);
fout.tie(NULL);
while (fin >> op >> s) {
if (op == 0) {
insert(rad, s);
}
else if (op == 1) {
erase(rad, s);
}
else if (op == 2) {
fout << count(rad, s) << '\n';
}
else {
fout << lcp(rad, s) << '\n';
}
}
return 0;
}