Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define PB push_back
- #define MP make_pair
- using namespace std;
- typedef long long ll;
- struct node {
- node *p[100];
- int count, leaf;
- node() {
- count = leaf = 0;
- memset(p, 0, sizeof(p));
- }
- };
- class trie {
- node *root;
- public:
- trie(){
- root = new node();
- }
- void insert(string s){
- node *x = root;
- int length = s.length();
- for (int i = 0; i < length; i++) {
- if (x->p[s[i] - 'a'] == NULL) {
- x->p[s[i] - 'a'] = new node();
- }
- x = x->p[s[i] - 'a'];
- x->count++;
- }
- x->leaf++;
- }
- void erase(string s){
- node *x = root;
- int length = s.length();
- for (int i = 0; i < length; i++) {
- x = x->p[s[i] - 'a'];
- x->count--;
- }
- x->leaf--;
- }
- int find(string s){
- node *x = root;
- int length = s.length();
- for (int i = 0; i <length; ++i){
- if(x->p[s[i] - 'a'] == NULL){
- return 0;
- }
- x = x->p[s[i] - 'a'];
- }
- return (x->count - x->leaf);
- }
- };
- int main() {
- ios::sync_with_stdio(0);
- cin.tie(0);
- int n;
- cin >> n;
- vector<string> a(n);
- trie t;
- for (int i = 0; i < n; ++ i) {
- cin >> a[i];
- cout << t.find(a[i]) << "\n";
- t.insert(a[i]);
- }
- }
Add Comment
Please, Sign In to add comment