Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- //#define int long long
- using namespace std;
- struct node{
- node *next[26];
- int cnt;
- node(){
- for(int i = 0; i < 26; ++i){
- next[i] = nullptr;
- }
- cnt = 0;
- }
- };
- node *root = new node();
- void add(string& input){
- node *curr = root;
- for(int i = 0; i < input.size(); ++i){
- if(curr->next[input[i] - 'a'] == nullptr){
- curr->next[input[i] - 'a'] = new node();
- }
- curr = curr->next[input[i] - 'a'];
- curr->cnt++;
- }
- root->cnt++;
- }
- int search(string &input){
- int ans = 0;
- node *temp = root;
- for(auto el : input){
- ans += temp->cnt;
- temp = temp->next[el - 'a'];
- }
- return ans;
- }
- int cnt(const string &s){
- int ans = root->cnt;
- node *temp = root;
- for(int i = 0; i < s.size(); ++i){
- if(temp->next[s[i] - 'a'] == nullptr){
- return ans;
- }else{
- ans += temp->cnt;
- temp = temp->next[s[i] - 'a'];
- }
- }
- return ans;
- }
- signed main() {
- ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
- int n, m;
- cin >> n;
- vector<string> v(n);
- unordered_map<string, int> input;
- for(int i = 0; i < n; ++i) cin >> v[i];
- cin >> m;
- vector<string> d(m);
- for(int i = 0; i < m; ++i){
- cin >> d[i];
- input[d[i]] = 1;
- }
- for(auto el : v){
- add(el);
- if(input[el] == 1){;
- input[el] = search(el) + 1;
- }
- }
- for(auto el : input){
- if(input[el.first] == 1){
- input[el.first] = cnt(el.first);
- }
- }
- for(int i = 0; i < m; ++i){
- cout << input[d[i]] << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement