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

题目地址


注意点:

  • search方法由于需要返回多种状态,应当使用int并保存重复情况.

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int SIZE=1000010,MAXN=55;
int trie[SIZE][26],tot=1;
bool ed[SIZE],vis[SIZE];
void insert(char* str){
    int len=strlen(str),p=1;
    for(int k=0;k<len;k++){
        int ch=str[k]-'a';
        if(trie[p][ch]==0)trie[p][ch]=++tot;
        p=trie[p][ch];
    }
    ed[p]=true;
}
int search(char* str){
    int len=strlen(str),p=1;
    for(int k=0;k<len;k++){
        p=trie[p][str[k]-'a'];
        if(!p)return 0;
    }
    if(ed[p]&&vis[p])return 2;
    vis[p]=1;
    return ed[p];
}
char str[SIZE];
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%s",&str);
        insert(str);
    }
    int m;
    scanf("%d",&m);
    for(int i=1;i<=m;i++){
        scanf("%s",&str);
        int tmp=search(str);
        if(!tmp)printf("WRONG
");
        else if(tmp==1)printf("OK
");
        else if(tmp==2)printf("REPEAT
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/zbsy-wwx/p/11680616.html