Dictionary tree Templates

Source: Internet
Author: User
Tags cmath

    • Find out if the string has already appeared
1 //finds whether the current string has occurred in the given string2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cstdio>6#include <cmath>7#include <string>8#include <deque>9#include <map>Ten #defineINF 0x3f3f3f3f One #defineFRE () freopen ("In.txt", "R", stdin) A  - using namespacestd; -typedefLong Longll; the Const intMAXN = 1e5 +5; - intTot,n; - inttrie[maxn][ -]; - //bool ISW[MAXN];//Query the whole word with +  - voidInsert (Char* STR,intRT) + { A      for(inti =0; Str[i]; i++) at     { -         intx = Str[i]-'a'; -         if(Trie[rt][x] = =0)//The currently inserted letter did not appear at the same node as before -         { -TRIE[RT][X] = tot++;//letters in and out of the new location, otherwise not processed -         } inRT = Trie[rt][x];//prepare for insertion of the next letter -     } to     //Isw[rt] = true;//The tail node that marks the last letter of the word, used when querying the entire word + } -  the  * BOOL_find (Char*STR,intRT) $ {Panax Notoginseng      for(inti =0; Str[i]; i++) -     { the         intx = Str[i]-'a'; +         if(Trie[rt][x] = =0)return false;//The X-letter with RT as the head node does not exist and returns 0 ART = Trie[rt][x];//prepare to query the next letter the     } +     return true; - } $  $  - Chars[ A]; - intMain () the { -tot =0;Wuyi     intRT =1; thescanf"%d",&n); -      for(inti =1; I <= N; i++) Wu     { -scanf"%s", s); About Insert (S, RT); $     } -scanf"%d",&n); -      for(inti =1; I <= N; i++) -     { Ascanf"%s",&s); +         if(_find (S, RT)) theprintf"yes\n"); -         Else $printf"no\n"); the     } the     return 0; the}
    • Statistics prefix and number of occurrences
1 //number of occurrences of the query prefix2#include <iostream>3#include <cstring>4#include <algorithm>5#include <cstdio>6#include <cmath>7#include <string>8#include <deque>9#include <map>Ten #defineINF 0x3f3f3f3f One #defineFRE () freopen ("In.txt", "R", stdin) A  - using namespacestd; -typedefLong Longll; the Const intMAXN = 1e5 +5; - inttrie[400001][ -],len,root,tot,sum[400001]; - BOOLp; - intn,m; + Chars[ One]; -  + voidInsert () A { atlen=strlen (s); -root=0; -      for(intI=0; i<len;i++) -     { -         intid=s[i]-'a'; -         if(!trie[root][id]) trie[root][id]=++tot; insum[trie[root][id]]++;//prefix is moved one position to save -root=Trie[root][id]; to     } + } - intSearch () the { *root=0; $len=strlen (s);Panax Notoginseng      for(intI=0; i<len;i++) -     { the         intid=s[i]-'a'; +         if(!trie[root][id])return 0; Aroot=Trie[root][id]; the}//After this loop, root becomes the next position in the position where the last letter of the prefix is prefixed +     returnSum[root];//because the prefix is moved to a save, sum[root at this time] is the number of times the required prefix appears - } $ intMain () $ { -scanf"%d",&n); -      for(intI=1; i<=n;i++) the     { -Cin>>s;Wuyi Insert (); the     } -scanf"%d",&m); Wu      for(intI=1; i<=m;i++) -     { AboutCin>s; $printf"%d\n", search ()); -     } -}

Dictionary tree Templates

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.