The positive solution is a dictionary tree, the use of linked lists to achieve a data structure, the construction method and purple book on the two-tree similar. Because the memory of this problem is relatively tight, so the memory problem needs to be solved, but if the recursive release of memory will lead to inefficient, the solution is to open a memory pool (array), each update subscript can be reused.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm>using namespace Std;int t,n,k;struct pa{Char s[15]; int Len;}; BOOL CMP (PA A,pa b) {return a.len>b.len;} struct trie{trie *next[15];}; Trie *root;trie All_trie[1000000];bool built (char *s,int len) {bool OK = true; Trie *p = root, *q; for (int i=0;i<len;i++) {int id = s[i]-' 0 '; if (p->next[id]==null) {ok = false; Q = &all_trie[k++]; for (int j=0;j<10;j++) q->next[j] = NULL; P->next[id] = q; p = p->next[id]; } else {p = p->next[id]; }} return OK; int main () {scanf ("%d", &t); while (t--) {scanf ("%d", &n); PA s[10005]; k = 0; bool OK = true; root = &all_trie[k++]; for (int i=0;i<10;i++) root->next[i] = NULL; for (int i=0;i<n;i++) {scanf ("%s", S[I].S); s[I].len = strlen (S[I].S); } sort (s,s+n,cmp); for (int i=0;i<n;i++) if (built (S[i].s,s[i].len)) {ok = false; } if (OK) printf ("yes\n"); else printf ("no\n"); } return 0;}
Phone List (HDOJ-1671) (Tire tree)