52分WA求调

P8306 【模板】字典树

chuazen @ 2024-07-23 19:59:07

字典树模板题,52分,不知为何WA

提交记录见https://www.luogu.com.cn/record/168084454

代码如下:

#include<bits/stdc++.h>
using namespace std;
const int N=3*1e6+5;
int T,n,m,trie[N][65],end_trie[N],total;
char s[N];
bool bo;
int change(char x){
    if(x>='A'&&x<='Z')return int(x-'A');
    else if(x>='a'&&x<='z')return int(x-'a'+26);
    else return int(x-'0'+52); 
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        for(int i=0;i<=total;i++)for(int j=1;j<=65;j++)trie[i][j]=0;
        for(int i=0;i<=total;i++)end_trie[i]=0;
        total=0;
        for(int i=1;i<=n;i++){
            scanf("%s",s);
            int p=0;
            for(int j=0;j<strlen(s);j++){
                int x=change(s[j]);
                if(trie[p][x]==0)total++,trie[p][x]=total;
                p=trie[p][x];
                end_trie[p]++;
            }
        }
        for(int i=1;i<=m;i++){
            scanf("%s",s);
            int p=0;
            bo=1;
            for(int j=0;j<strlen(s);j++){
                int x=change(s[j]);
                if(trie[p][x]==0){
                    bo=0;
                    printf("0\n");
                    break;
                }
                p=trie[p][x];
            }
            if(bo==1)printf("%d\n",end_trie[p]);
        }
    }
    return 0;
}

by youkasgs_wyb @ 2024-07-23 20:03:52

while(T--){
        scanf("%d %d",&n,&m);
        for(int i=0;i<=total;i++)for(int j=0;j<65;j++)trie[i][j]=0;改成这样
        for(int i=0;i<=total;i++)end_trie[i]=0;

by chuazen @ 2024-11-20 22:48:12

@youkasgs_wyb %%%


by youkasgs_wyb @ 2024-11-21 07:06:07

@chuazen不是哥们,四个月前


|