title

trie树

#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
const int LEN =110;
const int N =100010;
char s[LEN];
int end[N];//表示当前节点是否为结束节点 
int trie[N][26],tot=1;//初始化,假设字符串由小写字母组成 
int n;
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
	while(isdigit(ch)) 	x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return f?-x:x;
}
inline void insert(char* s){
	int len=strlen(s),p=1;
	for(register int i=0;i<len;++i){
		int ch=s[i]-'a';
		if(!trie[p][ch])	trie[p][ch]=++tot;
		p=trie[p][ch];
	}
	end[p]=true;
}
inline bool search(char* s){
	int len=strlen(s),p=1;
	for(register int i=0;i<len;++i){
		p=trie[p][s[i]-'a'];
		if(!p)	return false;
	}
	return end[p];
}
signed main(){
	n=read();
	for(register int i=1;i<=n;++i){
		int tmp=read();
		scanf("%s",s);
		if(tmp==1)	insert(s);
		else if(search(s))	printf("Find it!\n");
		else	printf("No way\n");
	}
	return 0;
}
posted @ 2018-09-10 01:39  Horrigue_JyowYang  阅读(90)  评论(0编辑  收藏  举报