Problem:
Slates
Numerous stone slates have been discovered in the ruins of the ancient nation of Iwashiro. Researchers have found that each slate has a single word inscribed on it. However, due to weathering over the years, some of the slates seem to be difficult to decipher for the following reasons.
In some cases, only one of the letters of the word on the slate is covered with moss, making it impossible to figure out the word.
The left side of the slate is missing (chipped), and there may have been some text written there (we cannot grasp more than or equal to 0 letters on the left side of the slate).
The right side of the slate is missing (chipped), and there may have been some text written there (we cannot grasp more than or equat to 0 letters on the right side of the slate).
There is at most one place where moss grows on the slate. In addition, although moss may grow on the chipped slate, both sides of the slate are not chipped at the same time.
Researchers have a dictionary of words known from surveys prior to the discovery of the slates. However, when guessing the original word from a slate with moss and chips due to the effects of weathering, it is not immediately clear how many words in the dictionary apply.
Write a program to count how many words in the given dictionary might fit for given a slate.
Input
The input is given in the following format.
N
M
word
1
word
2
:
word
N
slate
1
slate
2
:
slate
M
In the first line, the number of words in the dictionary
N
(1 ≤
N
≤ 50000), the number of slates
M
(1 ≤
M
≤ 50000) are given. The following
N
line gives the words
word
i
. A word is a character string with a length of 1 or more and 200 or less, including only lowercase letters. Note that the
N
words are all different. The following
M
lines give the string
slate
i
that represents the information for each slate.
slate
i
is a character string with a length of 1 or more and 200 or less, including lowercase letters, "?", And "*". ? represents a moss-covered character. At most one ? appears in one string. If the string starts with *, it means that the left side of the slate is missing. If the string ends with *, it indicates that the right side of the slate is missing. * does not appear except at the beginning or end of the string, and does not appear on both sides at the same time. A string with only one * is never given.
The total number of characters in the string given in the input does not exceed 3000000.
Output
For each slate, output the number of words on a single line.
Sample Input 1
5 4
aloe
apple
apricot
cactus
cat
apple
ap*
*e
ca?*
Sample Output 1
1
2
2
2

Submitted solutions:

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


#define NUM 50005

int N,M;
string WORD[NUM],rev_WORD[NUM];

int calc(const string slate){

	int left,right;
	string pattern = slate;

	if(pattern[pattern.length()-1] == '*'){

		pattern = pattern.substr(0,pattern.length()-1);
		left = lower_bound(WORD,WORD+N,pattern)-WORD;
		pattern.push_back('z'+1);
		right = lower_bound(WORD,WORD+N,pattern)-WORD;
		return right-left;

	}else if(pattern[0] == '*'){

		pattern = pattern.substr(1);
		reverse(pattern.begin(),pattern.end());
		left = lower_bound(rev_WORD,rev_WORD+N,pattern)-rev_WORD;
		pattern.push_back('z'+1);
		right = lower_bound(rev_WORD,rev_WORD+N,pattern)-rev_WORD;
		return right-left;

	}else{

		left = lower_bound(WORD,WORD+N,pattern)-WORD;
		if(WORD[left] == slate){
			return 1;
		}else{
			return 0;
		}

	}
}


int main(){

	scanf("%d %d",&N,&M);

	getchar();

	for(int i = 0; i < N; i++){
		getline(cin,WORD[i]);
		rev_WORD[i] = WORD[i];
		reverse(rev_WORD[i].begin(),rev_WORD[i].end());
	}
	sort(WORD,WORD+N);
	sort(rev_WORD,rev_WORD+N);

	string slate;
	int loc,ans;

	for(int loop = 0; loop < M; loop++){
		getline(cin,slate);

		loc = -1;

		for(int i = 0; i < slate.length(); i++){
			if(slate[i] == '?'){
				loc = i;
				break;
			}
		}

		if(loc == -1){
			printf("%d\n",calc(slate));
			continue;
		}

		ans = 0;
		for(int i = 0; i < 26; i++){
			slate[loc] = 'a'+i;
			ans += calc(slate);
		}

		printf("%d\n",ans);
	}

	return 0;
}
accuracy: 1
time_ms: 240
memory_kb: 13464
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;
// #include<atcoder/all>
// using mint = atcoder::modint998244353;
using ld = long double;
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(int)(n);++i)
template<typename T>bool chmin(T&a,T b){return b<a?(a=b,1):0;}
template<typename T>bool chmax(T&a,T b){return b>a?(a=b,1):0;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,M;
    cin>>N>>M;
    vector<string> A,RA;
    rep(i,N){
        string s;
        cin>>s;
        A.push_back(s);
        reverse(all(s));
        RA.push_back(s);
    }
    sort(all(A));
    sort(all(RA));

    rep(_,M){
        string s;
        cin>>s;
        int sw=0;
        if(s[0]=='*'){
            reverse(all(s));
            swap(A,RA);
            sw=1;
        }
        int ans=0;
        if(s.back()=='*'){
            s.pop_back();
            if(s.back()=='?') s.pop_back();
            if(s.empty()) ans=N;
            else{
                if(auto it=find(all(s),'?');it!=s.end()){
                    rep(i,26) {
                        *it='a'+i;
                        ans+=lower_bound(all(A),s+'~')-lower_bound(all(A),s);
                    }
                }
                else{
                    ans+=lower_bound(all(A),s+'~')-lower_bound(all(A),s);
                }
            }
        }
        else{
            if(auto it=find(all(s),'?');it!=s.end()){
                rep(i,26) {
                    *it='a'+i;
                    ans+=upper_bound(all(A),s)-lower_bound(all(A),s);
                }
            }
            else{
                ans+=upper_bound(all(A),s)-lower_bound(all(A),s);
            }
        }

        if(sw) swap(A,RA);

        cout<<ans<<"\n";
    }
}
accuracy: 1
time_ms: 110
memory_kb: 12144
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    int n, m;
    cin >> n >> m;
    vector<string> ws(n);
    set<string> origin;
    for(int i = 0; i < n; ++i) {
        cin >> ws[i];
        origin.insert(ws[i]);
    }
    auto rws = ws;
    for(auto& s : rws) reverse(begin(s), end(s));
    sort(begin(ws), end(ws));
    sort(begin(rws), end(rws));

    while(m--) {
        string ss;
        cin >> ss;
        bool left = ss[0] == '*', right = ss.back() == '*';
        string s;
        for(auto c : ss) {
            if(c != '*') s += c;
        }

        ll ans = 0;
        auto get_cnt = [](vector<string> const& ws, string& s) {
            auto l = lower_bound(begin(ws), end(ws), s);
            s.back() += 1;
            auto r = lower_bound(begin(ws), end(ws), s);
            s.back() -= 1;
            return r - l;
        };
        if(left) {
            reverse(begin(s), end(s));
            ans += get_cnt(rws, s);
        } else if(right) {
            ans += get_cnt(ws, s);
        } else {
            ans += origin.count(s);
        }

        for(int i = 0; i < (int)s.size(); ++i) {
            if(s[i] == '?') {
                for(char c = 'a'; c <= 'z'; ++c) {
                    s[i] = c;
                    if(left) {
                        ans += get_cnt(rws, s);
                    } else if(right) {
                        ans += get_cnt(ws, s);
                    } else {
                        ans += origin.count(s);
                    }
                }
                break;
            }
        }

        cout << ans << endl;
    }
}
accuracy: 1
time_ms: 220
memory_kb: 18044
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define int long long

/*
6:51~7:05
13:07~13:20
20:24~
*/

signed main(){
	int n,q;cin>>n>>q;
	vector<string> s(n);
	for(auto&&e:s)cin>>e;
	vector<string> rs;
	for(auto e:s){
		reverse(e.begin(),e.end());
		rs.push_back(e);
	}

	sort(s.begin(),s.end());

	sort(rs.begin(),rs.end());

	while(q--){
		string t;cin>>t;
		int m=t.size();
		if(t[0]=='*'){
			reverse(t.begin(),t.end());
			t[m-1]='{';
			string t2=t;t2.pop_back();
			int idx=find(t.begin(),t.end(),'?')-t.begin();
			swap(s,rs);
			if(idx==m){
				cout<<upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t2)<<endl;
			}else{
				int ans=0;
				for(char c='a';c<='z';c++){
					t[idx]=c;t2[idx]=c;
					ans+=upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t2);
				}
				cout<<ans<<endl;
			}
			swap(s,rs);
		}else if(t[m-1]=='*'){
			t[m-1]='{';
			string t2=t;t2.pop_back();
			int idx=find(t.begin(),t.end(),'?')-t.begin();

			if(idx==m){
				cout<<upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t2)<<endl;
			}else{
				int ans=0;
				for(char c='a';c<='z';c++){
					t[idx]=c;t2[idx]=c;
					ans+=upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t2);
				}
				cout<<ans<<endl;
			}
		}else{
			int idx=find(t.begin(),t.end(),'?')-t.begin();

			if(idx==m){
				cout<<upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t)<<endl;
			}else{
				int ans=0;
				for(char c='a';c<='z';c++){
					t[idx]=c;
					ans+=upper_bound(s.begin(),s.end(),t)-lower_bound(s.begin(),s.end(),t);
				}
				cout<<ans<<endl;
			}
		}
	}
}
accuracy: 1
time_ms: 190
memory_kb: 11960
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
  constexpr int BP_SIZE = 50005;
  constexpr long long MOD = 1000000007;
  constexpr long long BASE = 222222222;
  vector<long long> base_pow(BP_SIZE, 1);
  for(int i = 1; i < BP_SIZE; i++) {
    base_pow[i] = base_pow[i-1]*BASE%MOD;
  }
  int N, M;
  cin >> N >> M;
  map<long long, int> cnt, pref, suff;
  for(int i = 0; i < N; i++) {
    string word;
    cin >> word;
    int L = word.size();
    long long hp = 0, hs = 0;
    for(int j = 0; j < L; j++) {
      hp += base_pow[j]*word[j];
      hp %= MOD;
      hs *= BASE;
      hs += word[L-j-1];
      hs %= MOD;
      pref[hp]++, suff[hs]++;
    }
    cnt[hp]++;
  }
  auto query = [&](string s, map<long long, int> &mp) {
    long long h = 0;
    for(int i = 0; i < s.size(); i++) {
      h += base_pow[i]*s[i];
      h %= MOD;
    }
    int q = s.find('?');
    if(q == string::npos) return mp[h];
    int ret = 0;
    for(char c = 'a'; c <= 'z'; c++) {
      h -= base_pow[q]*s[q];
      s[q] = c;
      h += base_pow[q]*s[q];
      h += MOD*MOD;
      h %= MOD;
      ret += mp[h];
    }
    return ret;
  };
  for(int i = 0; i < M; i++) {
    string slate;
    cin >> slate;
    int L = slate.size();
    if(slate.front() == '*') {
      cout << query(slate.substr(1, L-1), suff) << endl;
    }else if(slate.back() == '*') {
      cout << query(slate.substr(0, L-1), pref) << endl;
    }else {
      cout << query(slate, cnt) << endl;
    }
  }
}
accuracy: 0.1
time_ms: 370
memory_kb: 49708
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
    int n,m;
    cin>>n>>m;
    vector<string> s(n),ss(n);
    for(int i = 0;i<n;i++){
        cin>>s[i];
        ss[i] = s[i];
        reverse(ss[i].begin(),ss[i].end());
    }
    sort(s.begin(),s.end());
    sort(ss.begin(),ss.end());
    for(int q = 0;q<m;q++){
        int ans = 0;
        string t;
        cin>>t;
        int k = -1;
        int ni = -1;
        string now = "";
        for(int i = 0;i<t.size();i++){
            if(t[i]=='*') k = i;
            else now += t[i];
            if(t[i]=='?') ni = i;
        }
        if(k==0) reverse(now.begin(),now.end());
        if(ni==-1){
            if(k==0){
                int nni = lower_bound(ss.begin(),ss.end(),now) - ss.begin();
                now += 'a' + 27;
                int nnj = lower_bound(ss.begin(),ss.end(),now) - ss.begin();
                ans += nnj - nni;
            }else{
                int nni = lower_bound(s.begin(),s.end(),now) - s.begin();
                if(k!=-1) now += 'a' + 27;
                int nnj = upper_bound(s.begin(),s.end(),now) - s.begin();
                ans += nnj - nni;
            }
        }else{
            ni = -1;
            for(int i = 0;i<now.size();i++) if(now[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                now[ni] = 'a' + i;
                if(k==0){
                    int nni = lower_bound(ss.begin(),ss.end(),now) - ss.begin();
                    now += 'a' + 27;
                    int nnj = lower_bound(ss.begin(),ss.end(),now) - ss.begin();
                    ans += nnj - nni;
                }else{
                    int nni = lower_bound(s.begin(),s.end(),now) - s.begin();
                    if(k!=-1) now += 'a' + 27;
                    int nnj = upper_bound(s.begin(),s.end(),now) - s.begin();
                    ans += nnj - nni;
                }
                if(k!=-1) now.pop_back();
            }
        }
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 190
memory_kb: 11696
final_rank: 2

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll b2 = 101;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll m2 = m3;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            int ni = s[i] - 'a';
            sum[i+1] = (sum[i]*B%mod+(ni))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            ni = -1;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            assert(ni!=-1);
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 420
memory_kb: 49716
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll = long long;

const ll b2 = 101;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll m2 = m4;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            int ni = s[i] - 'a';
            sum[i+1] = (sum[i]*B%mod+(ni))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            ni = -1;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            assert(ni!=-1);
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 420
memory_kb: 49628
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll b2 = 101;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll m2 = m4;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            ni = -1;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            assert(ni!=-1);
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 430
memory_kb: 49620
final_rank: 18

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll b2 = 101;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll m2 = m4;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 420
memory_kb: 49600
final_rank: 15

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll b2 = 29;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll m2 = m4;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 380
memory_kb: 49332
final_rank: 11

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll m2 = 1000000007;
const ll b2 = 29;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair((int)s.size(),h2.get())]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair((int)use.size(),h.get())]<<endl;
            }else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair((int)use.size(),h.get())]<<endl;
            }else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair((int)use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 390
memory_kb: 49412
final_rank: 12

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll m2 = 1000000007;
const ll b2 = 31;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair(s.size(),h2.get(0,s.size()))]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair(use.size(),h.get())]<<endl;
            }
            else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair(use.size(),h.get())]<<endl;
            }
            else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair(use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 390
memory_kb: 49592
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll m2 = 1000000007;
const ll b2 = 29;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair(s.size(),h2.get(0,s.size()))]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair(use.size(),h.get())]<<endl;
            }
            else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair(use.size(),h.get())]<<endl;
            }
            else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair(use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 390
memory_kb: 49600
final_rank: 14

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll m2 = 1000000007;
const ll b2 = 1009;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair(s.size(),h2.get(0,s.size()))]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get())]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair(use.size(),h.get())]<<endl;
            }
            else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair(use.size(),h.get())]<<endl;
            }
            else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair(use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 450
memory_kb: 49448
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

const ll m2 = 1000000007;
const ll b2 = 1009;
const ll m3 = 1000000009;
const ll b3 = 2009;
const ll m4 = 2147483587;
const ll b4 = 2147483583;
//https://gist.github.com/privet-kitty/295ac9202b7abb3039b493f8238bf40f#file-modulus-random-base-pair64-txt
struct rhash{
    int n;
    ll mod,B;
    vector<ll> sum;
    vector<ll> powb;
    rhash(string &s,ll mod,ll B):mod(mod),B(B){
        n = s.size();
        sum = vector<ll>(n+1,0);
        powb = vector<ll>(n+1,1);
        for(int i = 0;i<n;i++){
            sum[i+1] = (sum[i]*B%mod+(s[i]))%mod;
            powb[i+1] = (powb[i]*B)%mod;
        }
    }
    ll get(int l,int r){
        ll tmp = sum[r] - sum[l]*powb[r-l]%mod;
        if(tmp<0) tmp += mod;
        return tmp;
    }
    ll get(){
        return get(0,n);
    }
};

int main(){
    int n,m;
    cin>>n>>m;
    map<pair<int,ll>,int> s1,s2,s3;
    for(int i = 0;i<n;i++){
        string s;
        cin>>s;
        rhash h2(s,m2,b2);
        s3[make_pair(s.size(),h2.get(0,s.size()))]++;
        string now = "";
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            int k = now.size();
            s1[make_pair(k,h.get(0,k))]++;
        }
        now = "";
        reverse(s.begin(),s.end());
        for(int j = 0;j<s.size();j++){
            now += s[j];
            rhash h(now,m2,b2);
            s2[make_pair((int)now.size(),h.get())]++;
        }
    }
    for(int q = 0;q<m;q++){
        string s;
        cin>>s;
        int ni = -1;
        int k = -1;
        string use = "";
        for(int i = 0;i<s.size();i++){
            if(s[i]=='*') k = i;
            else use += s[i];
            if(s[i]=='?') ni = i;
        }
        if(ni==-1){ 
            if(k==s.size()-1) {
                rhash h(use,m2,b2);
                cout<<s1[make_pair(use.size(),h.get())]<<endl;
            }
            else if(k==-1) {
                rhash h(use,m2,b2);
                cout<<s3[make_pair(use.size(),h.get())]<<endl;
            }
            else{
                reverse(use.begin(),use.end()); 
                rhash h(use,m2,b2);
                cout<<s2[make_pair(use.size(),h.get())]<<endl;
            }
        }else{
            if(k==0) reverse(use.begin(),use.end());
            int ans = 0;
            for(int i = 0;i<use.size();i++) if(use[i]=='?') ni = i;
            for(int i = 0;i<26;i++){
                use[ni] = 'a' + i;
                rhash h(use,m2,b2);
                int kk = use.size();
                pair<int,ll> now = make_pair(kk,h.get());
                if(k==-1) ans += s3[now];
                else if(k==0) ans += s2[now];
                else ans += s1[now];
            }
            cout<<ans<<endl;
        }   
    }
}
accuracy: 0.1
time_ms: 450
memory_kb: 49616
final_rank: 20

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct E{
	unsigned short c;
	unsigned short csum;
	char c1;
	char vssize;	
	unsigned short * vs;
};
vector<E> vs[204];
 
void fadd(string& str,int p1,int p2){
	if(str.size()<=p1){
		vs[p1][p2].c++;
		vs[p1][p2].csum++;
		
	}else{
		vs[p1][p2].csum++;
		//cout<<p1<<" "<<p2<<" "<<vs[p1][p2].csum<<endl;
		int hit=-1;
		unsigned short *v=vs[p1][p2].vs;
		char s1=vs[p1][p2].vssize;
		for(int i=0;i<s1;i++){
			if(vs[p1+1][v[i]].c1==str[p1]){
				hit=v[i];
				break;
			}
		}
		if(hit==-1){
			E e1;
			e1.c=0;
			e1.csum=0;
			e1.c1=str[p1];
			e1.vssize=0;
			e1.vs=(unsigned short *)malloc(sizeof(unsigned short));
			hit=vs[p1+1].size();
			vs[p1+1].push_back(e1);
			vs[p1][p2].vssize++;
			s1=vs[p1][p2].vssize;
			unsigned short *t=(unsigned short *)realloc(vs[p1][p2].vs,sizeof(unsigned short)*(size_t)s1);
			if(vs[p1][p2].vs!=t){
				vs[p1][p2].vs=t;
			}
			vs[p1][p2].vs[s1-1]=hit;
		}
		//cout<<" ?p1 "<<p1<<" p2 "<<p2<<" str"<<str[p1]<<" vs="<<v.size()<<" hit="<<hit<<endl;
		fadd(str,p1+1,hit);
	}
}
unsigned short searchTree(string str,int p1,int p2){
	unsigned short res=0;
	if(str.size()<=p1){
		res+=vs[p1][p2].c;	
	}else{
		char c1=str[p1];
 
		unsigned short * v=vs[p1][p2].vs;
		char s1=vs[p1][p2].vssize;
		if(c1=='?'){
			for(int i=0;i<s1;i++){
				//cout<<"?"<<p1<<" "<<p2<<" "<<v[i]<<" "<<res<<endl;
				res+=searchTree(str,p1+1,(int)v[i]);
			}
		}else if(c1=='*'){
			res+=vs[p1][p2].csum;
				//cout<<"* p1"<<p1<<" p2"<<p2<<" res"<<res<<endl; 
		}else{
			for(int i=0;i<s1;i++){
				if(vs[p1+1][v[i]].c1==c1){
					res+=searchTree(str,p1+1,(int)v[i]);
					break;
				}
			}
		}
		//cout<<"c1"<<c1<<" p1"<<p1<<" p2"<<p2<<" csum"<<vs[p1][p2].csum<<endl;
	}
	return res;
}
struct E2{
	unsigned short no;
	string str;
};
vector<string> strs;
vector<E2> vs2;
int main() {
	int n,m;
	cin>>n>>m;
	E e1;
	e1.c=0;
	e1.csum=0;
	e1.vssize=0;
	e1.vs=(unsigned short *)malloc(sizeof(unsigned short));
	vs[0].push_back(e1);
	for(int i=0;i<n;i++){
		string str;
		cin>>str;
		fadd(str,0,0);
		reverse(str.begin(),str.end());
		strs.push_back(str);
		//fadd(str,0,0);
	}
 	unsigned short ans[50003];
	for(int i=0;i<m;i++){
		string str;
		cin>>str;
		if(str[0]=='*'){
			reverse(str.begin(),str.end());
			E2 e2;
			e2.no=i;
			e2.str=str;
			vs2.push_back(e2);
		}else{
			ans[i]=searchTree(str,0,0);
		}
	}
	for(int i=0;i<203;i++){
		for(int j=0;j<vs[i].size();j++){
			free(vs[i][j].vs);
		}
		vs[i].clear();
	}
	
	e1.vs=(unsigned short *)malloc(sizeof(unsigned short));
	vs[0].push_back(e1);
	for(int i=0;i<n;i++){
		fadd(strs[i],0,0);
	}
	for(int i=0;i<vs2.size();i++){
		E2 e2=vs2[i];
		ans[e2.no]=searchTree(e2.str,0,0);
	}
	for(int i=0;i<m;i++){
		cout<<ans[i]<<endl;
	}
	
	for(int i=0;i<203;i++){
		for(int j=0;j<vs[i].size();j++){
			free(vs[i][j].vs);
		}
		vs[i].clear();
	}
	return 0;
}
accuracy: 1
time_ms: 1160
memory_kb: 140440
final_rank: 6

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct E{
	unsigned short int c;
	unsigned short int cr;
	unsigned short int csum;
	unsigned short int csumr;
	char c1;
	vector<unsigned short int> vs;
};
vector<E> vs[211];

void fadd(string& str,int p1,int p2,bool rev){
	if(str.size()<=p1){
		if(rev==true){
			vs[p1][p2].cr++;
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].c++;
			vs[p1][p2].csum++;
		}
	}else{
		if(rev==true){
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].csum++;
		}
		//cout<<p1<<" "<<p2<<" "<<vs[p1][p2].csum<<endl;
		int hit=-1;
		vector<unsigned short int>& v=vs[p1][p2].vs;
		for(int i=0;i<v.size();i++){
			if(vs[p1+1][(int)v[i]].c1==str[p1]){
				hit=(int)v[i];
				break;
			}
		}
		if(hit==-1){
			E e1;
			e1.c=0;
			e1.cr=0;
			e1.csum=0;
			e1.csumr=0;
			e1.c1=str[p1];
			hit=vs[p1+1].size();
			
			vs[p1+1].push_back(e1);
			vs[p1][p2].vs.push_back(hit);
		}
		//cout<<" ?p1 "<<p1<<" p2 "<<p2<<" str"<<str[p1]<<" vs="<<v.size()<<" hit="<<hit<<endl;
		fadd(str,p1+1,hit,rev);
	}
}
unsigned short int searchTree(string str,int p1,int p2,bool rev){
	unsigned short int res=0;
	if(str.size()<=p1){
		if(rev==true){
			res+=vs[p1][p2].cr;
		}else{
			res+=vs[p1][p2].c;
		}
	}else{
		char c1=str[p1];
		
		vector<unsigned short int>& v=vs[p1][p2].vs;
		if(c1=='?'){
			for(int i=0;i<v.size();i++){
				//cout<<"?"<<p1<<" "<<p2<<" "<<v[i]<<" "<<res<<endl;
				res+=searchTree(str,p1+1,(int)v[i],rev);
			}
		}else if(c1=='*'){
			if(rev==true){
				res+=vs[p1][p2].csumr;
			}else{
				res+=vs[p1][p2].csum;
				//cout<<"* p1"<<p1<<" p2"<<p2<<" res"<<res<<endl;
			}
			
		}else{
			for(int i=0;i<v.size();i++){
				if(vs[p1+1][(int)v[i]].c1==c1){
					res+=searchTree(str,p1+1,(int)v[i],rev);
					break;
				}
			}
		}
		//cout<<"c1"<<c1<<" p1"<<p1<<" p2"<<p2<<" csum"<<vs[p1][p2].csum<<endl;
	}
	return res;
}

int main() {
	int n,m;
	cin>>n>>m;
	E e1;
	e1.c=0;
	e1.cr=0;
	e1.csum=0;
	e1.csumr=0;
	vs[0].push_back(e1);
	for(int i=0;i<n;i++){
		string str;
		cin>>str;
		fadd(str,0,0,false);
		reverse(str.begin(),str.end());
		fadd(str,0,0,true);
	}

	for(int i=0;i<m;i++){
		string str;
		cin>>str;
		if(str[0]=='*'){
			reverse(str.begin(),str.end());
			cout<<searchTree(str,0,0,true)<<endl;
		}else{
			cout<<searchTree(str,0,0,false)<<endl;
		}
	}
	return 0;
}
accuracy: 0.13333333333333333
time_ms: 170
memory_kb: 21052
final_rank: 8

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct E{
	unsigned short int c;
	unsigned short int cr;
	unsigned short int csum;
	unsigned short int csumr;
	char c1;
	vector<unsigned short int> vs;
};
vector<E> vs[211];

void fadd(string& str,int p1,int p2,bool rev){
	if(str.size()<=p1){
		if(rev==true){
			vs[p1][p2].cr++;
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].c++;
			vs[p1][p2].csum++;
		}
	}else{
		if(rev==true){
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].csum++;
		}
		//cout<<p1<<" "<<p2<<" "<<vs[p1][p2].csum<<endl;
		int hit=-1;
		vector<unsigned short int>& v=vs[p1][p2].vs;
		for(int i=0;i<v.size();i++){
			if(vs[p1+1][(int)v[i]].c1==str[p1]){
				hit=v[i];
				break;
			}
		}
		if(hit==-1){
			E e1;
			e1.c=0;
			e1.cr=0;
			e1.csum=0;
			e1.csumr=0;
			e1.c1=str[p1];
			hit=vs[p1+1].size();
			
			vs[p1+1].push_back(e1);
			vs[p1][p2].vs.push_back(hit);
		}
		//cout<<" ?p1 "<<p1<<" p2 "<<p2<<" str"<<str[p1]<<" vs="<<v.size()<<" hit="<<hit<<endl;
		fadd(str,p1+1,hit,rev);
	}
}
unsigned short int searchTree(string str,int p1,int p2,bool rev){
	unsigned short int res=0;
	if(str.size()<=p1){
		if(rev==true){
			res+=vs[p1][p2].cr;
		}else{
			res+=vs[p1][p2].c;
		}
	}else{
		char c1=str[p1];
		
		vector<unsigned short int>& v=vs[p1][p2].vs;
		if(c1=='?'){
			for(int i=0;i<v.size();i++){
				//cout<<"?"<<p1<<" "<<p2<<" "<<v[i]<<" "<<res<<endl;
				res+=searchTree(str,p1+1,(int)v[i],rev);
			}
		}else if(c1=='*'){
			if(rev==true){
				res+=vs[p1][p2].csumr;
			}else{
				res+=vs[p1][p2].csum;
				//cout<<"* p1"<<p1<<" p2"<<p2<<" res"<<res<<endl;
			}
			
		}else{
			for(int i=0;i<v.size();i++){
				if(vs[p1+1][v[i]].c1==c1){
					res+=searchTree(str,p1+1,(int)v[i],rev);
					break;
				}
			}
		}
		//cout<<"c1"<<c1<<" p1"<<p1<<" p2"<<p2<<" csum"<<vs[p1][p2].csum<<endl;
	}
	return res;
}

int main() {
	int n,m;
	cin>>n>>m;
	E e1;
	e1.c=0;
	e1.cr=0;
	e1.csum=0;
	e1.csumr=0;
	vs[0].push_back(e1);
	for(int i=0;i<n;i++){
		string str;
		cin>>str;
		fadd(str,0,0,false);
		reverse(str.begin(),str.end());
		fadd(str,0,0,true);
	}

	
	for(int i=0;i<m;i++){
		string str;
		cin>>str;
		if(str[0]=='*'){
			reverse(str.begin(),str.end());
			cout<<searchTree(str,0,0,true)<<endl;
		}else{
			cout<<searchTree(str,0,0,false)<<endl;
		}
	}

	
	return 0;
}
accuracy: 0.13333333333333333
time_ms: 90
memory_kb: 21676
final_rank: 7

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct E{
	unsigned short c;
	unsigned short cr;
	unsigned short csum;
	unsigned short csumr;
	char c1;
	vector<unsigned short> vs;
};
vector<E> vs[204];
 
void fadd(string& str,int p1,int p2,bool rev){
	if(str.size()<=p1){
		if(rev==true){
			vs[p1][p2].cr++;
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].c++;
			vs[p1][p2].csum++;
		}
	}else{
		if(rev==true){
			vs[p1][p2].csumr++;
		}else{
			vs[p1][p2].csum++;
		}
		//cout<<p1<<" "<<p2<<" "<<vs[p1][p2].csum<<endl;
		int hit=-1;
		vector<unsigned short>& v=vs[p1][p2].vs;
		for(int i=0;i<v.size();i++){
			if(vs[p1+1][(int)v[i]].c1==str[p1]){
				hit=v[i];
				break;
			}
		}
		if(hit==-1){
			E e1;
			e1.c=0;
			e1.cr=0;
			e1.csum=0;
			e1.csumr=0;
			e1.c1=str[p1];
			hit=vs[p1+1].size();
 
			vs[p1+1].push_back(e1);
			vs[p1][p2].vs.push_back(hit);
		}
		//cout<<" ?p1 "<<p1<<" p2 "<<p2<<" str"<<str[p1]<<" vs="<<v.size()<<" hit="<<hit<<endl;
		fadd(str,p1+1,hit,rev);
	}
}
unsigned short searchTree(string str,int p1,int p2,bool rev){
	unsigned short res=0;
	if(str.size()<=p1){
		if(rev==true){
			res+=vs[p1][p2].cr;
		}else{
			res+=vs[p1][p2].c;
		}
	}else{
		char c1=str[p1];
 
		vector<unsigned short>& v=vs[p1][p2].vs;
		if(c1=='?'){
			for(int i=0;i<v.size();i++){
				//cout<<"?"<<p1<<" "<<p2<<" "<<v[i]<<" "<<res<<endl;
				res+=searchTree(str,p1+1,(int)v[i],rev);
			}
		}else if(c1=='*'){
			if(rev==true){
				res+=vs[p1][p2].csumr;
			}else{
				res+=vs[p1][p2].csum;
				//cout<<"* p1"<<p1<<" p2"<<p2<<" res"<<res<<endl;
			}
 
		}else{
			for(int i=0;i<v.size();i++){
				if(vs[p1+1][v[i]].c1==c1){
					res+=searchTree(str,p1+1,(int)v[i],rev);
					break;
				}
			}
		}
		//cout<<"c1"<<c1<<" p1"<<p1<<" p2"<<p2<<" csum"<<vs[p1][p2].csum<<endl;
	}
	return res;
}
 
int main() {
	int n,m;
	cin>>n>>m;
	E e1;
	e1.c=0;
	e1.cr=0;
	e1.csum=0;
	e1.csumr=0;
	vs[0].push_back(e1);
	for(int i=0;i<n;i++){
		string str;
		cin>>str;
		fadd(str,0,0,false);
 
 
		reverse(str.begin(),str.end());
		fadd(str,0,0,true);
	}
	//for(int i=1;i<8;i++){
	//	cout<<i<<" ,  ";
	//	for(int j=0;j<vs[i].size();j++){
	//		cout<<j<<" "<<vs[i][j].c1<<" "<<vs[i][j].csum<<" , ";
	//	}
	//	cout<<endl;
	//}
 
	for(int i=0;i<m;i++){
		string str;
		cin>>str;
		if(str[0]=='*'){
			reverse(str.begin(),str.end());
			cout<<searchTree(str,0,0,true)<<endl;
		}else{
			cout<<searchTree(str,0,0,false)<<endl;
		}
	}
 
 
	return 0;
}
accuracy: 0.13333333333333333
time_ms: 180
memory_kb: 21048
final_rank: 9