社区讨论
for循环赋0怎么写?
P8306【模板】字典树参与者 2已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @lwmt2vgx
- 此快照首次捕获于
- 2024/05/26 08:32 2 年前
- 此快照最后确认于
- 2024/05/26 10:21 2 年前
code:
CPP#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=2*1e6+10;
int tr[N][62];
int cnt[N];
string s;
int n,q;
int p,idx,l,j;
void insert(string s){
p=0;
for(int i=0;s[i];i++){
if(s[i]>='0'&&s[i]<='9')j=s[i]-'0';
else if(s[i]>='A'&&s[i]<='Z')j=s[i]-'A'+10;
else j=s[i]-'a'+36;
if(!tr[p][j])tr[p][j]=++idx;
p=tr[p][j];
cnt[p]++;
}
}
int query(string s){
p=0;
for(int i=0;s[i];i++){
if(s[i]>='0'&&s[i]<='9')j=s[i]-'0';
else if(s[i]>='A'&&s[i]<='Z')j=s[i]-'A'+10;
else j=s[i]-'a'+36;
if(!tr[p][j])return 0;
p=tr[p][j];
}return cnt[p];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T;
cin>>T;
while(T--){
cin>>n>>q;
while(n--){
cin>>s;
insert(s);
}
while(q--){
cin>>s;
cout<<query(s)<<"\n";
}
for(int i=0;i<=100000;i++){
cnt[i]=0;
for(int j=0;j<=61;j++){
tr[i][j]=0;
}
}
idx=0;
}
return 0;
}
是memset直接T#2#3#4#5,for循环,范围开大了超时,开小了WA
回复
共 1 条回复,欢迎继续交流。
正在加载回复...