Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <algorithm>
- #include <string>
- #include <vector>
- #include <cstdio>
- #include <map>
- #include <utility>
- #include <queue>
- #include <stack>
- #include <cstring>
- #include <deque>
- #include <bitset>
- #include <cstdlib>
- #define F first
- #define S second
- #define mp make_pair
- #define pb push_back
- #define INF (1<<30)
- typedef long long ll;
- typedef long double ld;
- using namespace std;
- const int MAXN = 260;
- struct vertex{
- int next = -1;
- bool ending = false;
- };
- vertex root;
- int sz = 1;
- vertex bor[MAXN]['z'+1] = {root};
- void add_string(const string s){
- int v = 0;
- int n = s.length();
- int ch;
- for(int i = 0; i < n; i++){
- ch = s[i];
- if(bor[v][ch].next==-1){
- sz++;
- bor[v][ch].next = sz - 1;
- }
- v = bor[v][ch].next;
- }
- bor[v][ch].ending = true;
- }
- bool used[MAXN] = {false};
- int ans = 0;
- void dfs(int v){
- used[v] = true;
- for(int ch = 'a'; ch <= 'z'; ch++){
- if(bor[v][ch].ending){
- ans++;
- }
- int to = bor[v][ch].next;
- if(to!=-1 && !used[to]){
- dfs(to);
- }
- }
- }
- int find_prefix(const string s){
- int n = s.length();
- int v = 0;
- int ch;
- ans = 0;
- for(int i = 0; i < n; i++){
- ch = s[i];
- if(bor[i][ch].next == -1){
- return 0;
- }
- v = bor[v][ch].next;
- }
- dfs(v);
- return ans;
- }
- int main(){
- int n;
- cin >> n;
- string s;
- for(int i = 0; i < n; i++){
- cin >> s;
- add_string(s);
- }
- int m;
- cin >> m;
- for(int i = 0; i < m; i++){
- cin >> s;
- cout << find_prefix(s) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement