16分求助

P8306 【模板】字典树

SHC_CENSORED @ 2024-07-16 20:56:33

记录

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define ACeasy return 0
const int N=999999;
int son[N][90];
int cnt[N],idx;
char name[6000];
int n,m;
char transform(char c){
    if(c>='A'&&c<='Z'){return c-'A';}
    if(c>='a'&&c<='z'){return c-'a'+26;}
    if(c>='0'&&c<='9'){return c-'0'+52;}
}
void insert(char str[]){
    int p=0;
    for(int i=0;str[i];i++){
        int u=transform(str[i]);
        if(!son[p][u]){
            son[p][u]=++idx;
        }
        p=son[p][u];
        cnt[p]++;
    }
}

int quary(char str[]){
    int p=0;
    for(int i=0;str[i];i++){
        int u=transform(str[i]);
        if(!son[p][u]){
            return 0;
        }
        p=son[p][u];
    }return cnt[p];
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0); 
    int T;
    cin>>T;
    while(T--){
        for(int i=1;i<=idx;i++){
            for(int j=1;j<=100;j++){
                son[i][j]=0;
            }
        }
        for(int i=1;i<=idx;i++){
            cnt[i]=0;
        }idx=0;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>name;
            insert(name);
        }
        for(int i=1;i<=m;i++){
            cin>>name;
            cout<<quary(name)<<endl;
        }
    }
    ACeasy;
} 

by SHC_CENSORED @ 2024-07-16 21:24:05

受Tgotp老师修改 已AC

此贴结


by wanwang @ 2024-07-20 08:29:30

@SHC_CENSORED 可以说一下是什么问题吗,我也是这种情况


by SHC_CENSORED @ 2024-07-20 09:05:30

初始化 @wanwang


by SHC_CENSORED @ 2024-07-20 09:06:02

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define ACeasy return 0
const int N=3e6+20;
int son[N][70];
int cnt[N],idx;
char name[N];
int n,m;
char transform(char c){
    if(c>='A'&&c<='Z'){return c-'A';}
    if(c>='a'&&c<='z'){return c-'a'+26;}
    if(c>='0'&&c<='9'){return c-'0'+52;}
}
void insert(char str[]){
    int p=0;
    for(int i=0;str[i];i++){
        int u=transform(str[i]);
        if(!son[p][u]){
            son[p][u]=++idx;
        }
        p=son[p][u];
        cnt[p]++;
    }
}

int quary(char str[]){
    int p=0;
    for(int i=0;str[i];i++){
        int u=transform(str[i]);
        if(!son[p][u]){
            return 0;
        }
        p=son[p][u];
    }return cnt[p];
}

signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0); 
    int T;
    cin>>T;
    while(T--){
        for(int i=0;i<=idx;i++){
            for(int j=0;j<=100;j++){
                son[i][j]=0;
            }
        }
        for(int i=0;i<=idx;i++){
            cnt[i]=0;
        }idx=0;
        cin>>n>>m;
        for(int i=1;i<=n;i++){
            cin>>name;
            insert(name);
        }
        for(int i=1;i<=m;i++){
            cin>>name;
            cout<<quary(name)<<endl;
        }
    }
    ACeasy;
} 

by wanwang @ 2024-07-20 09:10:18

@SHC_CENSORED 谢谢现在ac勒


|