-
Notifications
You must be signed in to change notification settings - Fork 1
/
H.cpp
58 lines (53 loc) · 1.19 KB
/
H.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+3;
int n,w[N],d;
string s[N];
long long f[27][52];
void init(){
memset(f,0,sizeof(f));
}
void input(){
cin>>n;
for (int i=1;i<=n;i++) cin>>w[i];
cin>>d;
for (int i=1;i<=d;i++) cin>>s[i];
}
void solve(){
long long res = 0;
for (int i=1;i<=d;i++){
int id = s[i][0] - 'a';
//cout<<id<<" "<<s[i][]
f[id][s[i].size()]++;
}
for (int i=1;i<=d;i++){
int id = s[i][0] - 'a';
f[id][s[i].size()]--;
if (s[i].size()==n){
long long temp = 1;
for (int j=0;j<s[i].size();j++){
int id = s[i][j] - 'a';
temp = temp * f[id][w[j+1]];
f[id][w[j+1]]--;
}
for (int j=0;j<s[i].size();j++){
int id = s[i][j] - 'a';
f[id][w[j+1]]++;
}
res = res + temp;
}
f[id][s[i].size()]++;
}
cout<<res<<'\n';
}
int main(){
//freopen("atm.inp","r",stdin);
int t;
cin>>t;
while (t--){
init();
input();
solve();
}
return 0;
}