Advertisement
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;
- //#define int long long
- #pragma comment(linker,"/STACK:1000000000,1000000000")
- const long long INF = 1e9 + 7;
- const int MAXN = 26 * 100000 + 100;
- const int N = 1e5 + 10;
- struct vertex {
- int next[26];
- int terminal = 0;
- } v[MAXN];
- int root = 0, top = 1;
- array<long long, MAXN> dp;
- void add(const string& s) {
- int cur = root;
- for (auto& i: s) {
- int x = i - 'a';
- if (v[cur].next[x] == 0) {
- v[cur].next[x] = top++;
- }
- cur = v[cur].next[x];
- ++dp[cur];
- }
- v[cur].terminal = 1;
- }
- long long get(const string& s) {
- int cur = root;
- for (auto& i: s) {
- int x = i - 'a';
- if (v[cur].next[x] == 0) {
- return 0;
- } else {
- cur = v[cur].next[x];
- }
- }
- return dp[cur];
- }
- signed main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.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
Advertisement