Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <algorithm>
- #include <iostream>
- #include <array>
- #include <vector>
- #include <numeric>
- #include <random>
- #include <chrono>
- #include <set>
- using namespace std;
- const int INF = 1e9 + 7;
- const int MAXN = 26 * 100000 + 100;
- const int N = 1e5 + 10;
- struct vertex {
- vertex *next[26];
- bool terminal;
- int cnt = 0;
- ~vertex() {
- for (int i = 0; i < 26; ++i) {
- delete next[i];
- }
- }
- };
- vertex* root = new vertex();
- void add(const string &s) {
- vertex* cur = root;
- for (auto i : s) {
- char x = i - 'a';
- if (cur->next[x] == nullptr) {
- cur->next[x] = new vertex();
- }
- cur = cur->next[x];
- ++cur->cnt;
- }
- cur->terminal = true;
- }
- int get(const string& s) {
- vertex *cur = root;
- for (auto& i : s) {
- char x = i - 'a';
- if (cur->next[x] == nullptr) {
- return 0;
- }
- cur = cur->next[x];
- }
- return cur->cnt;
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- int n;
- cin >> n;
- for (int i = 0; i < n; ++i) {
- string s;
- cin >> s;
- add(s);
- }
- int m;
- cin >> m;
- for (int i = 0; i < m; ++i) {
- int type;
- string s;
- cin >> type >> s;
- if (type == 1) {
- add(s);
- } else {
- cout << get(s) << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment