#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int CHARSET = 26, BASE = 'a', MAX_NODE = 100000;
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 insert1(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 tmp;
int main()
{
cout << tmp.root << endl;
char p[20];
for(int i = 0; i < 3; i++) {
gets(p);
tmp.insert1(p);
}
cout << "query" << endl;
gets(p);
bool res = tmp.query(p);
if(res == true) {
cout << "yes" << endl;
}
else{
cout << "no" << endl;
}
return 0;
}
Trie
最新推荐文章于 2022-07-07 17:01:16 发布