Test instructions: INSERT, insert, and query the word according to the prefix, query the word exists.
because before almost did a similar problem, plus this time, to test instructions understanding of not in place, so wrong many times, after the first to test instructions understand and then knock code, so will avoid after the modification A lot of unnecessary detail errors.
Idea: Trie tree, the root root node is connected to 26 identical nodes by the next pointer array, representing 26 English letters, corresponding to the first letter, the same as the subsequent letters. So the tree structure came out. Num in the struct is the number of words that record the prefix of the current string.
(1) Insertnode: Insert the word, if the prefix does not exist before, assign a node to it, if already exists, counter + 1.
(2) Delnode: Delete the word, find the corresponding prefix, so that its counter zeroed, or delete the node is the row, * but before deleting the counter should be recorded value, because the word prefix is deleted, the previous prefix corresponding counter should also be reduced (global variable temp is used to record this value So that the previous prefix is subtracted from this value in subsequent operations) * Because this is wrong many times qaq.
(3) Delnum: There is a delnode after the deletion of the word, so that the prefix of this word corresponding to the NUM value of the same reduction of temp.
Eg: deleted the word ABC prefix 2, temp is 2, then the number of words prefixed with A and B is also reduced by temp.
(4) Searchnode: query operation, find the str2 array prefix num value, if its >0, that is, output "Yes", conversely, "No ".
Problem Description
The bear has a magical dictionary, you can do the following three operations in it:
1、insert : 往神奇字典中插入一个单词2、delete: 在神奇字典中删除所有前缀等于给定字符串的单词3、search: 查询是否在神奇字典中有一个字符串的前缀等于给定的字符串
Input
There is only one set of test data. The first line enters a positive integer n (1≤n≤100000) n (1\leq N\leq 100000) n (1≤n≤100000), which represents the number of times the bear operates on the dictionary, followed by nnn lines, each line containing two strings, separated by spaces 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 a word with the given string prefix is present in the degree bear's dictionary, output yes or output No.
Sample Input
Copy
5insert helloinsert hehesearch hdelete hesearch Hello
Sample Output
Copy
YesNo
Paste the code (this is the game time changed a lot of code, write very messy, if the topic pulled into the Hangzhou electric OJ, I will change again):
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<algorithm>#defineN 26using namespacestd;structnode{CharC; intnum; structNode *next[n];};inttemp;structNode *Root;Charstr1[ -], str2[ -];structNode *CreateNode () {structNode *p = (structNode *)malloc(sizeof(structnode)); Pnum =1; for(inti =0; i < N; i++) P-Next[i] =NULL; returnp;}voidDelnum (structNode *r,intN) { intcur = str2[n]-'a'; if(R-next[cur] = = NULL | | r--next[cur]-num = =0|| N >= strlen (STR2)-1) { return ; } ifNum >= (R-Next[cur)temp) R-next[cur], num-=temp; ElseR-next[cur], num =0; Delnum (RNext[cur], n +1);}structNode *insertnode (structNode *r,intN) { intcur = str2[n]-'a'; if(N >=strlen (STR2)) { returnR; } structNode *p = rNext[cur]; if(R-next[cur] = =NULL) {R-Next[cur] =CreateNode (); } Else{(R, Next[cur, num) + +; } //printf ("%d", R-next[cur), num);Insertnode (R-Next[cur], n +1); returnR;}structNode *delnode (structNode *r,intN) { intcur = str2[n]-'a'; structNode *p = rNext[cur]; if(N >= strlen (STR2)-1) { //if (R-next[cur]! = NULL)//printf ("%d", R-next[cur), num); if(R-next[cur]! =NULL) {Temp= R, Next[cur]num; R-next[cur], num =0; R-Next[cur] =NULL; Delnum (Root,0); } returnR; } if(R-next[cur] = = NULL | | r--next[cur]-num = =0) returnR; //printf ("%d", R-next[cur), num);Delnode (R-Next[cur], n +1); returnR;}intSearchnode (structNode *r,intN) { intcur = str2[n]-'a'; structNode *p = rNext[cur]; if(R-next[cur] = = NULL | | r--next[cur]-num = =0) return 0; if(N >= strlen (STR2)-1) { //printf ("%d", R-next[cur), num); if(R-next[cur] = = NULL | | r--next[cur]-num = =0) return 0; ifNum >= (R-Next[cur)1) return 1; Else return 0; } //printf ("%d", R-next[cur), num); returnSearchnode (R-Next[cur], n +1);}intMain () {intN; Root=CreateNode (); scanf ("%d", &N); for(inti =0; I < n; i++) {scanf ("%s%s", str1, str2); if(str1[0] =='D') {Delnode (root,0); } Else if(str1[0] =='I') {Insertnode (root,0); } Else { if(Searchnode (Root,0) ==1) printf ("yes\n"); Elseprintf ("no\n"); } } return 0;}
2016 "Baidu Star"-Qualifying tournament (Astar Round1) problem C (trie tree)