yillin @ 2024-08-02 00:11:34
#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#define int long long
#define PII pair<int,int>
#define lson (p << 1)
#define rson (p << 1 | 1)
#define lowbit(x) x & -x
using namespace std;
using ll = long long;
const int maxn = 1e6 + 5;
const int INF1 = 0x3f3f3f3f;
const int Mod = 1e9 + 7;
const int MAXN = 3e6 + 5;
int n, q;
namespace trie
{
int next[MAXN][65], cnt;
//bool vis[MAXN];
int exist[MAXN];
int getnum(char x)
{
if (x >= 'A' && x <= 'Z')
{
return x - 'A';
}
else if (x >= 'a' && x <= 'z')
{
return x - 'a' + 26;
}
else
{
return x - '0' + 52;
}
}
void init()
{
/*memset(next,0,sizeof(next));
memset(exist,0,sizeof(exist));*/
for (int i = 0; i <= n + 10; i++)
{
for (int j = 0; j < 65; j++)
{
next[i][j] = 0;
}
}
for (int i = 0; i <= n + 10; i++)
{
exist[i] = 0;
}
cnt = 1;
}
void insert(const string& s)
{
int cur = 1;
for (auto ci : s)
{
int c = getnum(ci);
//尽可能重用之前的路径,如果做不到就新建节点
if (!next[cur][c])
{
next[cur][c] = ++cnt;
}
cur = next[cur][c];//继续向下找
exist[cur]++;
}
}
int find(const string& s)
{
int cur = 1, ans = 0;
for (auto ci : s)
{
int c = getnum(ci);
if (!next[cur][c])
return 0;
cur = next[cur][c];
}
ans = exist[cur];
return ans;
}
bool find_prefix(const string& s)
{
int cur = 1;
for (auto ci : s)
{
int c = getnum(ci);
//沿着前缀决定的路径向下找,找不到就退出
if (!next[cur][c])
return false;
cur = next[cur][c];//继续向下找
}
return true;
}
}
void solve()
{
trie::init();
string t;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
string s;
cin >> s;
trie::insert(s);
}
for (int i = 1; i <= q; i++)
{
cin >> t;
cout << trie::find(t) << endl;
}
return;
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int _;
cin >> _;
//_=1;
for (int i = 1; i <= _; i++)
{
solve();
}
return 0;
}
by aCssen @ 2024-08-02 00:20:24
清空要到 cnt 吧
by yillin @ 2024-08-02 09:20:24
@aCssen 调好了,谢谢大佬