#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
struct Node
{
int next[2];
Node ()
{
memset (next, 0, sizeof next);
}
} z[N];
int ans[N], cnt[N], nd, cnt2[N];
vector <int> a[N], b[N];
void cls()
{
nd = 0;
for (int i = 0; i < N; i ++)
z[i] = Node();
memset (cnt, 0, sizeof cnt);
}
void insert(vector <int> &a)
{
int len = a.size(), now = 0;
for (int i = 0; i < len; i ++)
{
if (!z[now].next[a[i]])
z[now].next[a[i]] = ++ nd, now = nd;
else
now = z[now].next[a[i]];
cnt2[now] ++;
}
cnt[now] ++;
}
int query(vector <int> &a)
{
int len = a.size(), now = 0, ans = 0;
for (int i = 0; i < len; i ++)
{
if (!z[now].next[a[i]])
return ans;
else
now = z[now].next[a[i]];
ans += cnt[now];
}
return ans - cnt[now] + cnt2[now];
}
int main()
{
int n, m;
cin >> n >> m;
cls();
for (int i = 1; i <= n; i ++)
{
int tot;
cin >> tot;
for (int j = 1; j <= tot; j ++)
{
int x;
cin >> x;
a[i].push_back(x);
}
insert(a[i]);
}
for (int i = 1; i <= m; i ++)
{
int tot;
cin >> tot;
for (int j = 1; j <= tot; j ++)
{
int x;
cin >> x;
b[i].push_back(x);
}
ans[i] = query(b[i]);
}
for (int i = 1; i <= m; i ++)
cout << ans[i] << '\n';
return 0;
}
洛谷 P2922 / bzoj1590【Trie】
最新推荐文章于 2025-05-07 17:18:49 发布