求助
查看原帖
求助
1250563
Celestinefly楼主2024/4/4 18:44
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
//trie tree
const int N=1e4+10;
int tree[N][26],cnt[N],idx;
int m,n;
//建树
void build(string s){
    int p=0;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(!tree[p][j]) tree[p][j]=++idx;
        p=tree[p][j];
    }
    cnt[p]++;
}

int query(string s){
    int p=0;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(!tree[p][j]) return 0;
        p=tree[p][j];
    }
    if(cnt[p]==1){
        cnt[p]++;
        return 1;
    }else if(cnt[p]==0){
        return 0;
    }else{
        return 2;
    }
}
int main(){
    cin>>n;
    while(n--){
        string name;
        cin>>name;
        build(name);
    }
    cin>>m;
    while(m--){
        string namex;
        cin>>namex;
        int x=query(namex);
        if(x==0) cout<<"WRONG"<<endl;
        else if(x==1) cout<<"OK"<<endl;
        else cout<<"REPEAT"<<endl;
    }
    return 0;
}

超时

2024/4/4 18:44
加载中...