有点问题...

P8306 【模板】字典树

manyc @ 2023-05-21 19:37:46

不知道哪错了

#include<bits/stdc++.h>
using namespace std;
const int N=3000005;
int tr[N][65],cnt[N],idx;
int va(char x){
    if(x>='a'&&x<='z') return x-'a';
    if(x>='A'&&x<='Z') return x-'A'+26;
    return x-'0'+52;
}
void insr(string s){
    int u=0;
    for(int i=0;s[i];i++){
        int x=va(s[i]);
        if(!tr[u][x]) tr[u][x]=++idx;
        u=tr[u][x];
        cnt[u]++;
    }
}
int qur(string s){
    int u=0;
    for(int i=0;s[i];i++){
        int x=va(s[i]);
        if(!tr[u][x]) return 0;
        u=tr[u][x];
    }
    return cnt[u]++;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        for(int i=0;i<=idx;i++){
            cnt[i]=0;
            for(int j=0;j<62;j++) tr[i][j]=0;
        }
        idx=0;
        for(int i=1;i<=n;i++){
            string s;
            cin>>s;
            insr(s);
        }
        while(m--){
            string s;
            cin>>s;
            cout<<qur(s)<<endl;
        }
    }
    return 0;
}

by jiangjiangQwQ @ 2023-05-21 20:05:01

第26行return cnt[u];即可。


by jiangjiangQwQ @ 2023-05-21 20:05:24

AC记录


by jiangjiangQwQ @ 2023-05-21 20:05:34

@manyc


by manyc @ 2023-05-21 20:13:29

@Super_excavator 感谢!


|