HDU1671 Phone List "Dictionary tree"

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=1671


Main topic:

Give you n a string to determine if there is a string in the N string that is the prefix of another string, if it exists

Output "NO", otherwise output "YES".


Ideas:

Create a dictionary tree that stores n strings in a dictionary tree Count the number of prefix occurrences. Find the N string again, if

The number of occurrences of the string >1, indicating that the repetition occurred two times, the output "NO". If each appears, the output is "YES".

This problem if every time do not delete the dictionary tree, clear space, will be super memory. So add the operation to empty the dictionary tree.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;char s[10010][11];struct trienode{int Count; struct Trienode *next[10];};    Trienode *root;void Create () {root = new Trienode;    memset (root->next,null,sizeof (Root->next)); Root->count = 0;}    void Insert (char *s) {Trienode *p, *q;    p = root;            while (*s) {if (p->next[*s-' 0 '] = = NULL) {q = new Trienode;            memset (q->next,null,sizeof (Q->next));            Q->count = 1;        p->next[*s-' 0 '] = q;        } else p->next[*s-' 0 ']->count++;        p = p->next[*s-' 0 '];    s++;    }}int Find (char *s) {Trienode *p;    p = root;        while (*s) {if (p->next[*s-' 0 '] = = NULL) return 0;        p = p->next[*s-' 0 '];    s++; } return p->count;}    int Dele (Trienode *p) {if (p = = NULL) return 0; for (int i = 0; i < ++i) if (P-&GT    Next[i]! = NULL) Dele (P->next[i]); Free (p);}    int main () {int t,n;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        Create ();            for (int i = 0; i < N; ++i) {scanf ("%s", S[i]);        Insert (S[i]);        } int flag = 1;        for (int i = 0; i < N; ++i) {if (Find (S[i]) > 1) flag = 0;        } if (flag) printf ("yes\n");        else printf ("no\n");    Dele (root); } return 0;}


HDU1671 Phone List "Dictionary 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.