Phone List (HDOJ-1671) (Tire tree)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.