Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <stack>
- #include <climits>
- #include <string>
- #include <set>
- #include <cmath>
- #include <map>
- #include <unordered_map>
- #include <numeric>
- #include <random>
- #include <memory>
- #include <chrono>
- #include <iterator>
- #include <functional>
- #include <unordered_set>
- #include <cassert>
- #include <cstring>
- #ifdef LOCAL
- #include "debug.h"
- #else
- #define debug(x...)
- #endif
- /*
- #pragma GCC optimize("Ofast")
- #pragma GCC optimize("O3")
- #pragma GCC optimize("unroll-loops")
- */
- //#define int ll
- using namespace std;
- typedef long long ll;
- typedef long double ld;
- typedef pair < int, int > pii;
- typedef pair < ll, ll > pll;
- #define sz(x) int((x).size())
- #ifndef LOCAL
- mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
- #else
- mt19937 rng(228);
- #endif
- const int N = 2e5 + 7;
- const int inf = INT_MAX / 2;
- const ll INF = LLONG_MAX / 3;
- const int MOD = 1e9 + 7;
- const double eps = 1e-6;
- const string cars[] = {"🚗", "🚕", "🚙"};
- struct Trie {
- vector < vector < int > > a = vector < vector < int > > (1, vector < int > (26));
- vector < int > cnt = vector < int > (1);
- void add(string& s) {
- int v = 0;
- for (char c : s) {
- int flag = (toupper(c) == c);
- c = tolower(c);
- int t = c - 'a';
- if (a[v][t]) {
- v = a[v][t];
- }
- else {
- a[v][t] = sz(a);
- v = sz(a);
- a.push_back(vector < int > (26));
- cnt.push_back(0);
- }
- cnt[v] += flag;
- }
- }
- int query(string& s) {
- int v = 0, res = 0;
- for (char c : s) {
- int t = c - 'a';
- if (a[v][t]) {
- v = a[v][t];
- }
- else {
- return 0;
- }
- }
- return cnt[v];
- }
- } trie;
- signed main() {
- #ifdef LOCAL
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- cout << fixed << setprecision(4);
- ios::sync_with_stdio(false);
- cin.tie();
- cout.tie();
- ll pw[55] = { 1 };
- for (int i = 1; i < 55; i++) {
- pw[i] = pw[i - 1] * 229;
- }
- int n;
- cin >> n;
- for (int i = 0; i < n; i++) {
- string s;
- cin >> s;
- string t = s;
- ll hp = 0, hs = 0;
- for (int i = 0; i < sz(s); i++) {
- hp += (s[i] - 'a' + 1) * pw[i];
- hs = hs * pw[1] + (s[sz(s) - i - 1] - 'a' + 1);
- if (hs == hp) {
- t[i] = toupper(s[i]);
- }
- }
- trie.add(t);
- debug(t);
- }
- int m;
- cin >> m;
- for (int i = 0; i < m; i++) {
- string s;
- cin >> s;
- cout << trie.query(s) << "\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement