Trie
今晚花了点时间研究了这谜一般的代码。。。。
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; const int charset=26,base='a',max_node=100000; //charset 为字符集大小 //base 为字符集ASCII最小字符 //max_node为最大点数 struct Trie{ int tot,root,child[max_node][charset]; bool flag[max_node]; Trie(){ memset(child[1],0,sizeof(child[1])); flag[1]=false; root=tot=1; } void insert(const char *str){ int *cur=&root; for(const char *p=str;*p;++p){ cur=&child[*cur][*p-base]; if(*cur==0){ *cur=++tot; memset(child[tot],0,sizeof(child[tot])); flag[tot]=false; } } flag[*cur]=true; } bool query(const char *str){ int *cur=&root; for(const char *p=str;*p && *cur;++p)cur=&child[*cur][*p-base]; return (*cur && flag[*cur]); } }trie; int main(){ int i,j,k,m,n; char s[2000]; cin>>n; while(n--){ cin>>s; trie.insert(s); } cin>>n; while(n--){ cin>>s; cout<<trie.query(s)<<endl; } return 0; }