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)