tag: blog OS Io AR for 2014 SP Log On
# Include <cstdio> # include <algorithm> # include <cstring> # include <iostream> # include <map> # define max 15 using namespace STD; typedef struct trie_node {bool flag; // whether the subtree struct trie_node * Next [Max];} trie; void insert (trie * root, char * Str) {trie * P = root; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) {If (p-> next [STR [I]-'0'] = NULL) {trie * temp = (trie *) malloc (sizeof (trie )); for (Int J = 0; j <Max; j ++) temp-> Next [J] = NULL; temp-> flag = false; P-> next [STR [I]-'0'] = temp; P-> flag = true ;} P = p-> next [STR [I]-'0'];} bool find (trie * root, char * Str) {trie * P = root; int Len = strlen (STR); For (INT I = 0; I <Len; I ++) {If (p-> next [STR [I]-'0'] = NULL) return false; P = p-> next [STR [I]-'0'];} return p-> flag;} void del (trie * root) // release space {for (INT I = 0; I <Max; I ++) {If (root-> next [I]! = NULL) del (root-> next [I]);} Free (Root);} int main () {int t, n, I; char STR [10010] [15]; while (~ Scanf ("% d", & T) {While (t --) {scanf ("% d", & N); trie * root = (trie *) malloc (sizeof (trie); for (I = 0; I <Max; I ++) {root-> next [I] = NULL ;} root-> flag = false; for (I = 0; I <n; I ++) {scanf ("% s", STR [I]); insert (root, STR [I]) ;}for (I = 0; I <n; I ++) {If (find (root, STR [I]) break ;} if (I = N) printf ("Yes \ n"); else printf ("NO \ n"); del (Root) ;}} return 0 ;}
HDU 1671 phone list (Dictionary tree)