#include <bits/stdc++.h>
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii ;
typedef pair<ll, ll> pll ;
typedef vector<pii> vii ;
typedef vector<int> veci ;
typedef vector<pll> vll ;
typedef vector<ll> vecll;
// find_by_order order_of_key
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define F first
#define S second
#define pb push_back
#define endl '\n'
#define Mp make_pair
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " = " << x << endl
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("in.txt" , "r" , stdin) ; freopen("out.txt" , "w" , stdout);
#define lb lower_bound
#define ub upper_bound
#define for1(n) for(int i=1;i<=n;i++)
#define for0(n) for(int i=0;i<n;i++)
#define forn(n) for(int i=n;i>0;i--)
#define pq priority_queue <pii, vector<pii>, greater<pii>>
const ll mod =100109100129100151;// 998244353 ;// 1e9+9;
const int N=1e5+10,bs=7,sq=100;
int n,m;
vector<int>vecc;
ll pre[N],pw[N],suf[N];
string s[N];
vector<pll>vec;
int gt(char a){
return (a=='A' ? 1:(a=='C' ? 2:(a=='U' ? 3:4)));
}
int main(){
fast_io
pw[0]=1;
for1(N-1)pw[i]=pw[i-1]*bs%mod;
cin>>n>>m;
for1(n){
cin>>s[i];s[i]='0'+s[i];
int t=s[i].size();
for(int j=1;j<t;j++)s[i][j]=gt(s[i][j]);
if(t<=sq){
for(int j=1;j<t;j++)pre[j]=(pre[j-1]+s[i][j]*pw[j-1])%mod;
suf[t]=0;
for(int j=t-1;j>=1;j--)suf[j]=(suf[j+1]+s[i][j]*pw[t-j-1])%mod;
for(int j=1;j<t;j++){
for(int k=1;k<t;k++)
vec.pb({pre[j],suf[k]});
}
}
else
vecc.pb(i);
}
sort(all(vec));
while(m--){
string s1,s2;cin>>s1>>s2;
int t1=s1.size(),t2=s2.size();
for0(t1)s1[i]=gt(s1[i]);
for0(t2)s2[i]=gt(s2[i]);
ll h1=0,h2=0;
for0(t1)h1=(h1+s1[i]*pw[i])%mod;
for(int i=t2-1;i>=0;i--)h2=(h2+s2[i]*pw[t2-1-i])%mod;
int ans=ub(all(vec),Mp(h1,h2))-lb(all(vec),Mp(h1,h2));
for(int x:vecc)if(s[x].size()-1>max(t1,t2)){
int t=s[x].size();
bool o=1;
for(int j=0;j<t1;j++)o&=(s1[j]==s[x][j+1]);
if(o==0)continue;
for(int j=0;j<t2;j++)o&=(s2[j]==s[x][t-t2+j]);
ans+=o;
}
cout<<ans<<endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |