P2580 于是他错误的点名开始了

#include <bits/stdc++.h>
# define LL long long
using namespace std;

const int INF=0x7fffffff;

int n, m;

struct Trie{
    bool isword;
    int cnt;
    Trie* children[26];

    Trie(){
        isword=false;
        cnt=0;
        for(int i=0;i<26;++i){
            children[i]=nullptr;
        }
    }
};
Trie* root;

void build(string s){
    Trie* cur=root;
    for(char c:s){
        if(cur->children[c-'a']==nullptr){
            cur->children[c-'a']=new Trie();
        }
        cur=cur->children[c-'a'];
    }
    cur->isword=true;
}

int search(string s){
    Trie* cur=root;
    for(char c:s){
        if(cur->children[c-'a']==nullptr) return 0;
        cur=cur->children[c-'a'];
    }
    if(cur->isword==false) return 0;
    if(cur->cnt>0) return 2;
    cur->cnt=1;
    return 1;
}

int main(){
    scanf("%d", &n);
    root=new Trie();
    for(int i=1;i<=n;++i){
        string s;
        cin>>s;
        build(s);
    }
    scanf("%d", &m);
    for(int i=1;i<=m;++i){
        string s;
        cin>>s;
        int res=search(s);
        if(res==0) printf("WRONG\n");
        else if(res==2) printf("REPEAT\n");
        else printf("OK\n");
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/FEIIEF/p/12273481.html