Hdu1671Phone List-dictionary tree

Source: Internet
Author: User

Hdu1671Phone List
Simple dictionary tree
The space for time is too expensive. If there are multiple groups of data, er
Let's release it when it's used up.
[Cpp]
# Include <iostream>
Using namespace std;
 
Struct trie
{
Bool exist;
Int num;
Trie * next [10];
Trie ()
{
Exist = 0;
Num = 0;
For (int I = 0; I <10; I ++) next [I] = 0;
}
} * Root;
 
Bool insert (trie * p, char * s)
{
Int k = 0;
While (s [k]! = '\ 0 ')
{
If (! P-> next [s [k]-'0']) p-> next [s [k]-'0'] = new trie;
P = p-> next [s [k ++]-'0'];

If (p-> exist) return 0; // if This prefix is a phone number, the output "NO"
P-> num ++;
}
P-> exist = 1;
If (p-> num> 1) return 0; // if there is a number passing ,,

Return 1;
}
 
Void Free (trie * p)
{
For (int I = 0; I <10; I ++)
If (p-> next [I]) Free (p-> next [I]);
If (p) delete p;
}
 
Int main ()
{
Int T, n, I;
Bool Success;
Char t [11];
Scanf ("% d", & T );
While (T --)
{
Root = new trie;
Success = 1;
Scanf ("% d", & n );
For (I = 1; I <= n; I ++)
{
Scanf ("% s", t );
If (! Insert (root, t) {Success = 0; I ++; break ;}
}
For (; I <= n; I ++) scanf ("% s", t );
Printf (Success? "YES \ n": "NO \ n ");
Free (root );
}
Return 0;
}

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.