[Hackercup Round1 2] Autocomplete (Trie)

Source: Internet
Author: User

Title Link: https://www.facebook.com/hackercup/problems.php?pid=313229895540583&round=344496159068801

The main idea: see for yourself (in fact I don't know)

Bare trie tree, directly see if there is a node that must be inserted.

The code is too lame. Let's see.

1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <vector>5#include <cstdlib>6 using namespacestd;7 #defineCHARSET 268 9 Const intMax_node =10000000;Ten  One structtrienode{ A     intCh[charset]; - }; - Trienode Trie[max_node]; the intptr; - intans; -  - intT,n; + CharStr[max_node]; -  +  A voidinit () { atptr = ans =0; -      for(intI=0; i<charset;i++){ -trie[0].ch[i] =-1; -     } - } -  in intNewNode () { -ptr++; to      for(intI=0; i<charset;i++){ +Trie[ptr].ch[i] =-1; -     } the     returnptr; * } $ Panax Notoginseng voidInsertConst Char*str) { -     BOOLhasadded =false; the     intLen =strlen (str); +     intRT =0; A      for(intI=0; i<len;i++){ the         intid = str[i]-'a'; +         if(trie[rt].ch[id]==-1 ){ -Trie[rt].ch[id] =NewNode (); $             if( !hasadded) { $Ans = ans + i +1; -hasadded =true; - //printf ("+%d\n", i+1); the             } -         }WuyiRT =Trie[rt].ch[id]; the     } -     if(!hasadded) { WuAns = ans +Len; - //printf ("+%d\n", Len); About     } $ } -  - intMain () { - //freopen ("Input.txt", "R", stdin); A //freopen ("Output.txt", "w", stdout); +scanf"%d",&T); the      for(intCases =1; cases <= T; cases++){ -scanf"%d",&N); $ init (); the          for(intI=0; i<n;i++){ thescanf"%s", str); the Insert (str); the         } -printf"Case #%d:%d\n", Cases,ans); in     } the     return 0; the}

[Hackercup Round1 2] Autocomplete (Trie)

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.