蒟蒻求助

P8306 【模板】字典树

Obviathy @ 2022-05-03 18:49:04

评测记录

#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+5;
int t[N][70],f[N];
int n,q,tot=1;
char str[N];
void Insert(char *s){
    int p = 1,len = strlen(s);
    for(int i = 0;i < len;i ++){
        int c;
        if('A'<=s[i]&&s[i]<='Z')c = s[i]-'A';
        if('a'<=s[i]&&s[i]<='z')c = s[i]-'a'+26;
        else c = s[i] - '0' + 52;
        if(t[p][c] == 0)t[p][c] = ++tot;
        p = t[p][c];
        f[p] ++;
    }
}
int ans;
/*
void dfs(int x){
    f[x] = siz[x];
    for(int i = 0;i < 62;i ++){
        int v = t[x][i];
        if(v == 0)continue;
        dfs(v);
        f[x]+=f[v];
    }
}
*/
int solve(char *s){
    int len = strlen(s),p =1;
    for(int i = 0;i < len;i ++){
        int c;
        if('A'<=s[i]&&s[i]<='Z')c = s[i]-'A';
        if('a'<=s[i]&&s[i]<='z')c = s[i]-'a'+26;
        else c = s[i] - '0' + 52;
        if(t[p][c] == 0)return 0;
        p = t[p][c];
    }
    return f[p];
}
int main(){
    int T;
    cin >> T;
    while(T--){
        scanf("%d%d",&n,&q);
        for(int i = 0;i <= tot;i ++)f[i] = 0;
        for(int i = 0;i <= tot;i ++)for(int j = 0;j <= 66;j ++)t[i][j] = 0;
        tot=1;
        for(int i = 1;i <= n;i ++){
            scanf("%s",str);
            Insert(str);
        }
        for(int i = 1;i <= q;i ++){
            scanf("%s",str);
            ans = solve(str);
            printf("%d\n",ans);
        }
    }
    return 0;
}

大佬帮调一下吧,谢谢!


by Mr学霸君 @ 2022-05-03 18:55:46

诶等等,这个名字……山大附中的?


by Gokix @ 2022-05-03 19:01:07

@SDFZ

招摇过市,被识破了吧


by Gokix @ 2022-05-03 19:14:15

@SDFZ

Insert和solve里把字符转化成数字的那里第二个if应该是else if


by Obviathy @ 2022-05-03 19:40:32

@Gokix AC谢谢

名字是因为改的时候改了一半就确定了,之后要等一年才能修改,现在到了时间可以改了,但还没想好改啥


by Gokix @ 2022-05-03 19:45:48

@SDFZ

没事,没别的意思,就是顶着学校的名字像是学校官方公用账号啥的


|