蒟蒻求助,零分,各种错误......

P8306 【模板】字典树

haiming @ 2023-10-20 08:47:35

#include<bits/stdc++.h>
using namespace std;
int T,n,m,i,t,f[300100][70],a[100100];char s[1000100];
char zh(char a){
    if('0'<=a&&a<='9')return a-48;
    else if('A'<=a&&a<='Z')return a-65+10;
    else return a-97+36;
}
void tree(){
    int n=strlen(s),j=1;
    for(int i=0;i<n;i++){
        if(!f[j][zh(s[i])])f[j][zh(s[i])]=++t;
        j=f[j][zh(s[i])],a[j]++;
    }
}
int find(){
    int n=strlen(s),j=1;
    for(int i=0;i<n;i++)
        if(!f[j][zh(s[i])])return 0;
        else j=f[j][zh(s[i])];
    return a[j];
}
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        memset(f,0,sizeof(f));memset(a,0,sizeof(a));
        t=1;
        for(i=1;i<=n;i++){
            scanf("%s",s);
            tree();
        }
        for(i=1;i<=m;i++){
            scanf("%s",s);
            printf("%d\n",find());
        }
    } 
    return 0;
}

样例过的


by haiming @ 2023-10-20 08:59:25

#include<bits/stdc++.h>
using namespace std;
int T,n,m,i,j,t,f[300100][70],a[3000100];char s[3000100];
int zh(char a){
    if('0'<=a&&a<='9')return a-48;
    else if('A'<=a&&a<='Z')return a-65+10;
    else return a-97+36;
}
void tree(){
    int n=strlen(s),j=1;
    for(int i=0;i<n;i++){
        if(!f[j][zh(s[i])])f[j][zh(s[i])]=++t;
        j=f[j][zh(s[i])],a[j]++;
    }
}
int find(){
    int n=strlen(s),j=1;
    for(int i=0;i<n;i++){
        if(!f[j][zh(s[i])])return 0;
        j=f[j][zh(s[i])];
    }
    return a[j];
}
int main(){
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(i=1;i<=t;i++)
            for(j=0;j<70;j++)
                f[i][j]=0;
        for(i=1;i<=t;i++)a[i]=0;
        t=1;
        for(i=1;i<=n;i++){
            scanf("%s",s);
            tree();
        }
        for(i=1;i<=m;i++){
            scanf("%s",s);
            printf("%d\n",find());
        }
    } 
    return 0;
}

现在1 6 俩点RE了


by RQH1234 @ 2023-11-07 12:19:29

1G内存限制,我卡了好久QWQ(应该开到【3000000】【70】吧)


|