trie树0pts 求调教

P2922 [USACO08DEC] Secret Message G

btd,jbl
by livexcy @ 2023-08-19 17:42:49


调试+教导 没问题
by Kniqht @ 2023-08-23 10:27:45


@[Darling_zero_two](/user/754502) !
by kkxacj @ 2023-08-25 20:19:06


@[Darling_zero_two](/user/754502) 真内蒙会写代码 ```cpp #include<bits/stdc++.h> using namespace std; int n,m; int a[114514]; int cnt,sum[1145141],trie[1145141][3]; int sum1[1145141]; void insert(int len) { int root=0; for(int i=1;i<=len;i++) { if(!trie[root][a[i]]) trie[root][a[i]]=++cnt; root=trie[root][a[i]]; sum[root]++; } sum1[root]++; } int query(int len) { int root=0; int ans=0; int flag=0; for(int i=1;i<=len;i++) { if(!trie[root][a[i]]) { flag=1; break; } else root=trie[root][a[i]]; ans+=sum1[root]; } if(!flag) ans+=sum[root]-sum1[root]; return ans; } int main() { cin>>n>>m; for(int i=1;i<=n;i++) { int x; cin>>x; for(int j=1;j<=x;j++) cin>>a[j]; insert(x); } while(m--) { int x; cin>>x; for(int i=1;i<=x;i++) cin>>a[i]; cout<<query(x)<<endl; } } ```
by kkxacj @ 2023-08-25 20:20:26


@[kkxacj](/user/704089) 大佬%%%
by Darling_zero_two @ 2023-08-25 21:34:42


|