Node* New(){ Node* ret = &node[K++]; for (int i = 0; i < 26; i++) { ret->ne[i] = NULL; } ret->danger = false; return ret; }
voidinit(){ K = 0; root = New(); }
voidinsert(char* s){ char* p = s; Node* ptr = root; int id; while (*p) { id = *(p++) - 'a'; if (ptr->ne[id] == NULL) { ptr->ne[id] = New(); } ptr = ptr->ne[id]; } ptr->danger = true; }
booldfs(char* s, int tim){ // tim=0 means that it is the first time search, // otherwise it is the second time. char* p = s; Node* ptr = root; int id; while (*p) { id = *(p++) - 'a'; if (ptr->ne[id] == NULL) { returnfalse; // if the next pointer is null, return false. } ptr = ptr->ne[id]; // important. // if this node is a danger case, we search a second time. if (ptr->danger) { if (tim == 0 && *p && dfs(p, 1)) { returntrue; } } } // if the node is dangerous and the string has been completely search // return true. if (tim == 1 && ptr->danger) { returntrue; } else { returnfalse; } }
intmain(){ int n = 0; init(); while (gets(in[n])) { insert(in[n]); n++; } for (int i = 0; i < n; i++) { if (dfs(in[i], 0)) { puts(in[i]); } } return0; }