[洛谷P2580]于是他错误的点名开始了

题目大意:给你n个名字,然后m次点名。对于每次点名,如果该名字没有,则输出WRONG,如果该名字第一次被点,输出OK,如果该名字已经被点,则输出REPEAT。

解题思路:用字典树(Trie)保存,查询即可。对于多次被点名的人,我们在每个节点上加一个变量cnt,记录被点名的次数,如果$cntgeq 1$,说明该人已经被点名,输出WRONG即可。

C++ Code:

#include<cstdio>
using namespace std;
struct node{
	bool exist;
	int cnt;
	node* nxt[26];
	node():exist(false),cnt(0){
		for(int i=0;i<26;++i)nxt[i]=NULL;
	}
}*d;
int n;
char s[55];
void ins(char* s){
	node *p=d;
	for(int i=0;s[i];++i){
		int v=s[i]-'a';
		if(p->nxt[v]==NULL)p->nxt[v]=new node;
		p=p->nxt[v];
	}
	p->exist=true;
}
int query(char* s){
	node *p=d;
	for(int i=0;s[i];++i){
		int v=s[i]-'a';
		p=p->nxt[v];
		if(p==NULL)return 3;
	}
	if(p->exist){
		if(p->cnt++)return 2;
		return 1;
	}
	return 3;
}
int main(){
	d=new node;
	scanf("%d",&n);
	while(n--){
		scanf("%s",s);
		ins(s);
	}
	scanf("%d",&n);
	while(n--){
		scanf("%s",s);
		int ans=query(s);
		switch(ans){
			case 1:puts("OK");break;
			case 2:puts("REPEAT");break;
			case 3:puts("WRONG");
		}
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Mrsrz/p/7372943.html