Trie Tree---(INSERT, delete, query string)

Source: Internet
Author: User
Tags first string

HDU 5687

Problem description The bear has a magical dictionary, you can do the following three things in it:
1. Insert: Inserts a word into the Magic Dictionary
2. Delete: Remove all words in the Magic dictionary that are equal to the given string
3, Search: Whether the query in the Magic Dictionary has a string prefix equal to the given string input there is only one set of test data. The first line enters a positive integer< Span id= "mathjax-span-2" class= "Mrow" >n (1 ≤n≤ 100000) , which represents the number of times the bear operates on the dictionary, next n Row, each row contains two strings, separated by a space in the middle. The first string represents the associated action (including: INSERT, delete, or search). The second string represents the string specified after the related operation, and the second string is no longer than 30. The second string is made up of lowercase letters only. Output for each search operation, if the given string is prefixed with a word in the degree bear's dictionary, outputs Yes otherwise outputs No. Sample input5insert helloinsert hehesearch hdelete hesearch hellosample OutputYesNoSource2016 "Baidu Star"-Qualifying tournament (Astar   ROUND1) recommendliuyiding | We have carefully selected several similar problems for you:5689 5688 5686 5685 5684 idea: Use the trie tree to add the number of num tag strings to each node; code As follows:
#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespaceStd;typedefstructnode{intnum; Node*next[ -]; Node () {memset (Next,0,sizeof(next)); Num=0;///It's a good way to learn. Simple and practical initialization of the node's pretty code}}trie;Chardir[ +],s[ +];voidInsert (Node *root,Char*1) {Node*p=Root;  for(intI=0; T[n]; i++){        intx=s[i]-'a'; if(p->next[x]==NULL) P->next[x]=Newnode; P=p->Next[x]; P->num++; }}intSearch (Node *root,Char*s) {Node*p=Root;  for(intI=0; s[i];i++)    {        intx=s[i]-'a'; if(p->next[x]==NULL)return 0; P=p->Next[x]; }    returnP->num;}voidDelete (Node *root,Char*s,intCNT) {Node*p=Root;  for(intI=0; s[i];i++)    {        intx=s[i]-'a'; P=p->Next[x]; P->num-=cnt;//Deleting a node is nothing more than losing the number of words at the beginning of the current prefix.    }     for(intI=0;i< -; i++) P->next[i]=0;//and then all the children will be killed (roar good cruel)}intMain () {intN; scanf ("%d",&N); Trie*root=Newnode;  while(n--) {scanf ("%s%s", dir,s); if(dir[0]=='I') {Insert (root,s); }        Else if(dir[0]=='s')        {            if(Search (root,s)) printf ("yes\n"); Elseprintf ("no\n"); }        Else        {            intCnt=Search (root,s); if(CNT) Delete (ROOT,S,CNT); }    }    return 0;}

Trie Tree---(INSERT, delete, query string)

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.