Hello people..! In this post I will show you how to get started with solving Trie Tree based questions in competitive programming. Learning a data structure is different from solving competitive coding questions based on that data structure. In this post, I take up a very simple question so that your confidence is boosted.
We will look at the SPOJ problem – Phone List. It is a very straight forward problem. Just so that you don’t need to go to SPOJ in a new tab, I’m putting the problem statement here.
Problem Statement
Phone List Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:
• Emergency 911
• Alice 97 625 999
• Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
Input
The first line of input gives a single integer, 1 <= t <= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 <= n <= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.
Output
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
The Problem in terms of Trie Tree
We are supposed to check if any word is a prefix of any other or not. Now, there might be a hundred ways to solve this problem, but we will do this using a trie tree so that we can get confident in using the data structure, and so that we can attempt tougher ones based on a trie tree. All throughout my explanation, I will be referring to the implementation in my post on Trie Tree.
What we will do to solve this problem is that, we will insert the words into the trie tree one-by-one, and we will check for the prefix word criteria as we are inserting. Now, there are 2 cases.
Case – 1 – Prefix word is already inserted
This is the sample test case. Consider this test case –
So, what I said we will do is that, we will be inserting the words one-by-one. So, when we insert the word “face”, no problems occur. But while inserting the word “facebook”, we would travel to the nodes F → A → C → E. And at the node E, we would have some indication that this node is a leaf node, that is, some word ends here. In my implementation, this can be indicated by –
if (temp->occurrences.size() == 0) { // not a leaf node } else { // a leaf node, thus this is // the end of the prefix word }
If we encounter this situation, we know that the result will be NO.
Case 2 – Prefix word is about to be inserted
This is just the opposite of the previous case, consider the test case –
face
We won’t have any issues while inserting “facebook”. But when inserting “face”, we traverse F → A → C → E. And in the end of insertion, we see that there is a child node to the node E. Which means that there is a bigger word which ends somewhere deep down the trie tree. Which means that we just inserted the prefix word. We could check this by traversing the children array –
for (i = 0; i < ALPHABETS; ++i) { if (temp->children[i] != NULL) { // The newly inserted word is // prefix to another word } }
In this case too, the answer would be a NO. For every other case, the answer would be a YES.
So, try to code this problem. Take your code of the trie tree, remove the unnecessary things first, like the delete function or print function or any others which we won’t need. As fas as I know, the insert function is all that you will need. And try to make the changes required for the test cases.
Your insert function could return a true or a false, depending on whether the insertion encountered a prefix test case or not. Take time to think about it and try to code it. If you get stuck, you can refer to my code below –
A word of caution –
- Even if you did encounter a prefix word very early, don’t break out of the input, as you will disturb the input for the next test case.
I hope that you were able to solve this problem using a trie tree. It is simple and a prefect problem to start your trie tree streak in competitive coding. Feel free to comment if you have any doubts. If you have any bugs in your code, I’d be glad to help, but don’t comment your entire code in the comment, please leave Ideone or PasteBin links, or if you don’t want to show your code publicly, you can fill up the response form below to mail your code to me. I will respond as soon as I can. Keep practising… Happy Coding…! 😀
12 thoughts on “Trie Tree Practise – SPOJ – PHONELST”
Great blog!
http://ideone.com/Y7mRRY
Please tell me why this program is giving run time error?
It is because the first condition in the while loop should be –
not
You must use the “temp” variable there, not the “trieTree” variable. 🙂
Thank you so so much! I don’t know how I kept missing it!
Although there is another issue. On submitting your code and my corrected code, SPOJ is giving TLE for C++ 4.3.2
Why does calloc give no error but malloc gives segmentation fault?
Probably because of your variable mismatch in the if case, (answered above) calloc has got nothing to do with it 🙂
Probably there is some other reason. Because I got that error, when I replaced calloc with malloc in your code
Hi bud Thanks for such nice description 🙂 I coded the very same tho it is not giving me correct output.Like to my sol http://ideone.com/7Jhyb4 .I have one more question that you can add for beginners.
This –>> https://www.hackerearth.com/problem/algorithm/cost-of-data-11/
I found this useful.And please add some more light weight and easy questions if you can.Another question link
I’ve seen your code… You are not checking for the case when the prefix word is already inserted… When the prefix word is already inserted, you might encounter it while traversing
or, the word which you inserted was already inserted before… In which case, you must check for status of ‘leaf’ after traversing… Both these cases must be checked before leaving the procedure –
You can see your code with the neccessary corrections on this link… And thanks a lot for the suggestion…. I’ll surely add more easy questions… 🙂
Hi thanks for your reply 🙂 I am so sorry but I can’t imagine why do we need to check for leaf condition ? Please if you can elaborate when you have some spare time.I will keep giving you links for the topics that you have in your blog.Hoping that would be helpful to you 🙂
There are 2 scenarios where we would have to check the leaf condition –
Think about it… You will definetly get it… 😉
Hi I was in college and gave it a thought and I was able to get this.I would like to thank you for everything. 🙂
My pleasure… 🙂