( ̄y▽ ̄) ~ Smart phone iitimelimit:3000/1000 MS (java/others) menorylimit:32768/32768 K (java/others) 64-bit integer IO format:%i64d
Problem description in our phone address book, we can search for contacts by entering the phone number. If we enter a mobile phone number that is also a part of the other phone number prefix, the same will be displayed, prefixed with this mobile phone number of the person's information.only if you enter the number is unique, that is, the number entered is not a prefix of other numbers,, only one person's information is displayed.
Like what:
1. Lhboy 911
2. Bobo 10086
3. Hzz 10010
When you enter the number of any one of them, it will only show a person's information,
If this address book adds one more group: 4. Orz 1001011, then, when you enter 10010, the contacts found are Hzz and Orz.
Now the problem, give you an address book, ask you in the mobile phone number query, the contact information of the query is unique, the only word output yes, otherwise output No.
* Memory Overrun, try to release the memory ( ̄y▽ ̄) ~ ~Input the first line to enter an integer t, indicating that there is a T (1 <= T <= 40) group test case.
Each set of test cases first enters an integer n (1 <= n <= 10000), indicating that the address book has n messages.
Next there are n rows, each of which represents the phone number of each contact, with a maximum of 10 digits per phone number. Outputfor each test case with output "YES" If the list is consistent, or "NO" otherwise. Sampleinput
2381187625999811254265113123401234401008698346
Sampleoutput
NOYES
Solution:
Similar, with a dictionary tree, if you use a chain of the dictionary tree also need to free memory space, the number of dictionaries in the form of an array is not required,
Code:
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 using namespacestd;6typedefstructTrie7 {8 structtrie*node[Ten];9 intnum;Ten }trie_node; Onetrie_node*Head; A -trie_node*New_node () - { thetrie_node*H; - inti; -H= (trie_node*)malloc(sizeof(Trie_node)); - for(i=0; i<=9; i++) +h->node[i]=NULL; -h->num=0; + returnH; A } at - voidFree (trie_node*a) - { - if(A==null)return;//Freeing Memory - Else - { in for(intI=0;i<Ten; i++) -Free (a->node[i]); to } + Free(a); - } the intInsert_trie (Charstr[]) * { $Trie_node *T;Panax NotoginsengTrie_node *h=Head; - intI,len=strlen (STR), sign,j=0; the for(i=0; i<len;i++) + { Asign=str[i]-'0'; the if(h->node[sign]==NULL) + { -t=New_node (); $h->node[sign]=T; $j=1; - } -H=h->Node[sign]; the if(H->num) {j=0; Break;} - if(i==len-1) h->num=1;Wuyi } the returnJ; - } Wu intMain () - { About intt,n,i,j,sign; $ Charstr[ One]; -scanf"%d",&T); - while(t--) - { AHead=New_node (); +scanf"%d",&N); thesign=0; - for(i=0; i<n;i++) $ { thescanf"%s", STR); thej=Insert_trie (STR); the if(j==0) sign=1; the } - if(sign) printf ("no\n"); in Elseprintf"yes\n"); the Free (Head); the } About return 0; the}View Code
( ̄y▽ ̄) ~ Smart Phone II