[ACM] Hdu 1671 Phone List (Terry)

Source: Internet
Author: User

Phone List

Problem Descriptiongiven A list of phone numbers, determine if it is consistent in the sense this no number is the prefix of another. Let ' s say the phone catalogue listed these numbers:
1. Emergency 911
2. Alice 97 625 999
3. Bob 91 12 54 26
In this case, it's not possible-to-call Bob, because-the central would direct your-to-the-emergency line as soon as Y OU had dialled the first three digits of Bob ' s phone number. So the list would not being consistent.

Inputthe first line of input gives a single integer, 1 <= t <=, the number of test cases. Each test case is starts with N, the number of the phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with a unique phone number on each line. A phone number is a sequence of at the most ten digits.
Outputfor each test case with output "YES" If the list is consistent, or "NO" otherwise.
Sample Input
2391197625999911254265113123401234401234598346

Sample Output
NOYES

Source2008 "Insigma International Cup" Zhejiang Collegiate Programming Contest-warm up (3)



Problem Solving Ideas:

Infers if there is a string in the input string that is a different string of prefixes.


Create a dictionary tree. The key is to set a sign at the end of a string, that is, which letter ends. There are two scenarios to consider if there is a prefix to be inferred. The first is whether the current input string is the prefix of the string that was entered, but whether the string that was entered is the prefix of the current input string. In the previous case, when looking for the string, the first letter will be found to the last letter, the middle does not return no matter what value, indicating that the current string is the prefix. In the latter case, when the current string is found, it encounters a sign that the string has ended. Indicates that the string was the prefix of the current string. The symbol ending in the code is the CNT value of the node that holds the last letter in the string-1.


Code:

#include <iostream> #include <malloc.h> #include <algorithm> #include <string.h> #include <    stdio.h>using namespace Std;const int maxn=10;bool ok;char str[12];int t,n;struct trie{int cnt; Trie *NEXT[MAXN];};    Trie *root;void Createtrie (char *str) {int len=strlen (str);    Trie*p=root,*q;        for (int i=0;i<len;i++) {int id = str[i]-' 0 ';            if (p->next[id] = = NULL) {q = (Trie *) malloc (sizeof (Trie));            q->cnt = 1;            for (int j=0; j<maxn; ++j) q->next[j] = NULL;            P->next[id] = q;        p = p->next[id];        } else {p = p->next[id];    }} p->cnt=-1;//string at the end of the flag}int Findtrie (char *str) {int len=strlen (str);    Trie *p=root;        for (int i=0;i<len;i++) {int id=str[i]-' 0 '; if (p->next[id]==null) return 0;//not built tree if (p->next[id]->cnt==-1) return-1;//once string is the current string       Prefix p=p->next[id];             } return-1;//Current string is the prefix of the once string}void release (Trie *root)//Free space {for (int i=0;i<maxn;i++) {if (Root->next[i])    Release (Root->next[i]); } free (root);}    int main () {scanf ("%d", &t);        while (t--) {ok=1;        root= (trie*) malloc (sizeof (Trie)),//root for pointer type, requires space for (int i=0; i<10; ++i) root->next[i] = NULL;        scanf ("%d", &n);            while (n--) {scanf ("%s", str); if (Findtrie (str) ==-1) ok=0;//has a prefix if (!ok) continue;//is prefixed.        There is no need to build up the Createtrie (str);        } if (OK) printf ("yes\n");        else printf ("no\n");    Release (root); } return 0;}


Copyright notice: This article Bo Master original articles, blogs, without consent, may not be reproduced.

[ACM] Hdu 1671 Phone List (Terry)

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.