{"id":1775,"date":"2015-08-24T15:33:10","date_gmt":"2015-08-24T10:03:10","guid":{"rendered":"http:\/\/theoryofprogramming.azurewebsites.net\/?p=1775"},"modified":"2023-11-19T06:59:02","modified_gmt":"2023-11-19T06:59:02","slug":"trie-tree-practise-spoj-phonelst","status":"publish","type":"post","link":"https:\/\/theoryofcoding.com\/index.php\/2015\/08\/24\/trie-tree-practise-spoj-phonelst\/","title":{"rendered":"Trie Tree Practise &#8211; SPOJ &#8211; PHONELST"},"content":{"rendered":"\n<p>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.<br>We will look at the <a href=\"http:\/\/www.spoj.com\/problems\/PHONELST\/\">SPOJ problem &#8211; Phone List<\/a>. It is a very straight forward problem. Just so that you don&#8217;t need to go to SPOJ in a new tab, I&#8217;m putting the problem statement here.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Problem Statement<\/h3>\n\n\n\n<p>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\u2019s say the phone catalogue listed these numbers:<\/p>\n\n\n\n<p>\u2022 Emergency 911<\/p>\n\n\n\n<p>\u2022 Alice 97 625 999<\/p>\n\n\n\n<p>\u2022 Bob 91 12 54 26<\/p>\n\n\n\n<p>In this case, it\u2019s 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\u2019s phone number. So this list would not be consistent.<\/p>\n\n\n\n<p><strong>Input<\/strong><\/p>\n\n\n\n<p>The first line of input gives a single integer, 1 &lt;= t &lt;= 40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1 &lt;= n &lt;= 10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.<\/p>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<p>For each test case, output \u201cYES\u201d if the list is consistent, or \u201cNO\u201d otherwise.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Problem in terms of Trie Tree<\/h3>\n\n\n\n<p>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 <a href=\"http:\/\/theoryofprogramming.azurewebsites.net\/2015\/01\/16\/trie-trees-2\/\">Trie Tree<\/a>.<br>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.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Case &#8211; 1 &#8211; Prefix word is already inserted<\/h4>\n\n\n\n<p>This is the sample test case. Consider this test case &#8211;<\/p>\n\n\n<div class=\"su-box su-box-style-glass\" id=\"\" style=\"border-color:#000000;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#333333;color:#FFFFFF;border-top-left-radius:0px;border-top-right-radius:0px\">Input<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\nface<br \/>\nfacebook<br \/>\n<\/div><\/div>\n\n\n\n<p>So, what I said we will do is that, we will be inserting the words one-by-one. So, when we insert the word &#8220;face&#8221;, no problems occur. But while inserting the word &#8220;facebook&#8221;, we would travel to the nodes F \u2192 A \u2192 C \u2192 E. And at the&nbsp;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 &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nif (temp-&gt;occurrences.size() == 0) {\n\t\/\/ not a leaf node\n} else {\n\t\/\/ a leaf node, thus this is\n\t\/\/ the end of the prefix word\n}\n<\/pre><\/pre>\n\n\n\n<p>If we encounter this situation, we know that the result will be NO.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Case 2 &#8211; Prefix word is about to be inserted<\/h4>\n\n\n\n<p>This is just the opposite of the previous case, consider the test case &#8211;<\/p>\n\n\n<div class=\"su-box su-box-style-glass\" id=\"\" style=\"border-color:#000000;border-radius:0px;max-width:none\"><div class=\"su-box-title\" style=\"background-color:#333333;color:#FFFFFF;border-top-left-radius:0px;border-top-right-radius:0px\">Input<\/div><div class=\"su-box-content su-u-clearfix su-u-trim\" style=\"border-bottom-left-radius:0px;border-bottom-right-radius:0px\">\nfacebook<br \/>\nface<br \/>\n<\/div><\/div>\n\n\n\n<p>We won&#8217;t have any issues while inserting &#8220;facebook&#8221;. But when inserting &#8220;face&#8221;, we traverse&nbsp;F \u2192 A \u2192 C \u2192 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 &#8211;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\nfor (i = 0; i &amp;lt; ALPHABETS; ++i) {\n\tif (temp-&gt;children&#x5B;i] != NULL) {\n\t\t\/\/ The newly inserted word is\n\t\t\/\/ prefix to another word\n\t}\n}\n<\/pre><\/pre>\n\n\n\n<p>In this case too, the answer would be a NO. For every other case, the answer would be a YES.<\/p>\n\n\n\n<p>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&#8217;t need. As far as I know, the insert function is all that you will need. And try to make the changes required for the test cases.<br>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 &#8211;<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/VamsiSangam\/7fd133045bf427e0025e7f60b39beac1.js\"><\/script>\n\n\n\n<p>A word of caution &#8211;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Even if you did encounter a prefix word very early, don&#8217;t break out of the input, as you will disturb the input for the next test case.<\/li>\n<\/ul>\n\n\n\n<p>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&#8217;d be glad to help, but don&#8217;t comment your entire code in the comment, please leave Ideone or PasteBin links. I will respond as soon as I can. Keep practising&#8230; Happy Coding&#8230;! \ud83d\ude00<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello people..! In this post I will show you how to get started with solving Trie Tree based questions in competitive programming. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[105,106,112],"class_list":["post-1775","post","type-post","status-publish","format-standard","hentry","category-competitive-coding","tag-spoj","tag-spoj-editorial","tag-trie-tree"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1775","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/comments?post=1775"}],"version-history":[{"count":4,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1775\/revisions"}],"predecessor-version":[{"id":3856,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/posts\/1775\/revisions\/3856"}],"wp:attachment":[{"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/media?parent=1775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/categories?post=1775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/theoryofcoding.com\/index.php\/wp-json\/wp\/v2\/tags?post=1775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}