for循环赋0怎么写?

P8306 【模板】字典树

Typical_typhoon @ 2024-05-26 08:31:48

code:

#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


by gavinliu266 @ 2024-05-26 09:17:45

for(int i = 0; i <= idx; ++i)


by Typical_typhoon @ 2024-05-26 12:10:54

@gavinliu266 抱歉原来小脑萎缩了,谢谢大佬指点,已AC


|