字典树板子,36分求助,AC56 WA1234

P8306 【模板】字典树

OR_Boy @ 2023-01-30 16:44:15

36分求助,AC56 WA1234,

#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<math.h>
#include<string.h>
#include<bitset>
#define FOR(i,m,n) for(int i=m;i<=n;i++)
#define ll long long
#define mp(a,b) make_pair(a,b)
using namespace std;
int tl=0,a[3000007][127],n,m,ans[3000007],T;
string s;
void add(const string &s){
    int sum=0;
    FOR(i,0,s.size()-1){
        if(!a[sum][s[i]-'A'+1])a[sum][s[i]-'A'+1]=++tl;
        sum=a[sum][s[i]-'A'+1];
        ans[sum]++;
    }
}
int check(const string &s){
    int sum=0,res=0;
    FOR(i,0,s.size()-1){
        if(!a[sum][s[i]-'A'+1])return 0;
        sum=a[sum][s[i]-'A'+1];
    }
    return ans[sum];
}
int main(){
    cin>>T;
    while(T--){
        cin>>n>>m;
        FOR(i,1,n)cin>>s,add(s);
        FOR(i,1,m)cin>>s,cout<<check(s)<<endl;
        FOR(i,0,tl){
            FOR(j,1,126)a[i][j]=0;
            ans[i]=0;
        }
        tl=0;
    }
    return 0;
}

by Killer_joke @ 2023-01-30 17:01:34

'A'的ASCII码是65 '0'的ASCII码是48

-'A'+1会产生负数导致越界

@OR_Boy


by OR_Boy @ 2023-01-30 17:03:27

@Killer_joke 哦哦,谢谢,我以为输入一定是字母。话说这样的话不应该是RE吗,为什么会是WA


by Falling_Sakura @ 2023-08-15 14:33:31

@OR_Boy 同问


|