首页 > 代码库 > codevs 4189 字典
codevs 4189 字典
二次联通门 : codevs 4189 字典
/* codevs 4189 字典 Trie树 裸题 用指针写的.. */ #include <cstdio> #include <cstring> #include <cstdlib> void read (int &now) { now = 0; register char word = getchar (); while (word < ‘0‘ || word > ‘9‘) word = getchar (); while (word >= ‘0‘ && word <= ‘9‘) { now = now * 10 + word - ‘0‘; word = getchar (); } } struct Trie_Type { struct Trie { Trie *next[26]; bool End; bool flag; }; Trie Root; void Insert (char *line) { int Length = strlen (line); Trie *now = &Root; Trie *res; for (int i = 0; i < Length; i++) { int name = line[i] - ‘a‘; if (now->next[name] == NULL) { res = (Trie *) malloc (sizeof Root); res->flag = true; for (int j = 0; j < 26; j++) res->next[j] = NULL; now->next[name] = res; now = now->next[name]; } else { now->next[name]->flag = true; now = now->next[name]; } } } void Clear () { for (int i = 0; i < 26; i++) Root.next[i] = NULL; } bool Search (char *line) { int Length = strlen (line); Trie *now = &Root; int name; for (int i = 0; i < Length; i++) { name = line[i] - ‘a‘; now = now->next[name]; if (now == NULL) return false; } return true; } }; Trie_Type Make; int N; int main (int argc, char *argv[]) { read (N); Make.Clear (); register char word[10]; for (int i = 1; i <= N; i++) { scanf ("%s", word); Make.Insert (word); } read (N); while (N--) { scanf ("%s", word); printf (Make.Search (word) ? "YES" : "NO"); putchar (‘\n‘); } return 0; }
codevs 4189 字典
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。