Phone List (HDOJ-1671) (tire tree)
A positive solution is a dictionary tree that uses a linked list to construct a data structure similar to a binary tree in zishu. Because the memory for this question is relatively tight, you need to solve the memory problem. However, if you release the memory recursively, the efficiency will be low. The solution is to open a memory pool (array ), each time the subscript is updated, it can be reused.
#include
#include
#include
#includeusing 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
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