HDU 1671 Phone List (given n number string, ask if there is a string in the N number string is the prefix of the other string, if there is output no, otherwise yes)

Source: Internet
Author: User

1. Dynamic application of memory to be released after use, otherwise hyper-memory.

2. Code:

#include <cstdio> #include <cstring> #include <stdlib.h>using namespace std;struct node{int cnt;    Node *NEXT[10];        void Init () {cnt=0;        for (int i=0;i<10;i++) {next[i]=null; }    }};    Node *p_root;int n;void Insert (char s[]) {int Len=strlen (s);    Node *p=p_root;        for (int i=0;i<len;i++) {int num=s[i]-' 0 ';            if (p->next[num]==null) {p->next[num]= (node *) malloc (sizeof (node));        (*p->next[num]). Init ();        } p=p->next[num];    p->cnt++;    }}int Search (char s[]) {int Len=strlen (s);    Node *p=p_root;        for (int i=0;i<len;i++) {int num=s[i]-' 0 ';    p=p->next[num]; } return p->cnt;} void Destroy (Node *p) {for (int i=0;i<10;i++) {if (p->next[i]!=null) {Destroy (p->n        Ext[i]); }} free (P);}    int main () {int t;    scanf ("%d", &t);    Char s[10005][15];  while (t--)  {p_root= (node *) malloc (sizeof (node));        (*p_root). Init ();        scanf ("%d", &n);            for (int i=0;i<n;i++) {scanf ("%s", S[i]);        Insert (S[i]);        } int flag=0;                for (int i=0;i<n;i++) {if (Search (S[i]) >=2) {flag=1;            Break        }} if (flag) printf ("no\n");        else printf ("yes\n");    Destroy (P_root); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1671 Phone List (given n number string, ask if there is a string in the N number string is the prefix of the other string, if there is output no, otherwise yes)

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.