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;
}