Problem 1003 求助

kill2020 | 2016-05-15 22:22:22Author
#include <cstdio> #include <cstring> #include <cstdlib> #define MAX 26 typedef struct Trie { Trie *next[MAX]; int v; bool flag ; }Trie; Trie *root; void Ctrie(char *str) { int len = strlen(str); Trie *p = root,*q; for(int i = 0;i < len;i++) { int id = str[i] - 'a'; if(p->next[id] == NULL) { q = (Trie*)malloc(sizeof(Trie)); q->v = 1; q->flag = true; for(int j = 0;j < MAX;++j) q->next[j] = NULL; p->next[id] = q; p = p->next[id]; } else { p->next[id]->v++; p->flag = true; p = p->next[id]; } } return ; } int findT(char *str) { int len = strlen(str); Trie *p = root; for(int i = 0; i < len;++i) { int id = str[i] - 'a'; p = p->next[id]; if(p == NULL ) return 0; else if(p && p->flag == false) return 0; } return 1; } int del(Trie* T) { int i; if(T == NULL) return 0; for(i = 0; i < 26;i++) { if(T->next[i] != NULL) del(T->next[i]); } T->flag = false; return 0; } int find_del(char *str,int k) { Trie *p = root,*q; int len = strlen(str); for(int i = 0; i < len;++i) { int id = str[i] - 'a'; p = p->next[id]; p->v -= k; if(p->v == 0) { del(p); break; } } return 1; } int findTk(char *str) { int k; Trie *p = root; for(int i = 0; i < strlen(str);i++) { int id = str[i] - 'a'; if(p->next[id] == NULL ) return 0; p = p->next[id]; k = p->v; } return k; } int main() { int n,i; char instrc[8],str[35]; scanf("%d",&n); root = (Trie *)malloc(sizeof(Trie)); for(i = 0 ;i < 26;i++) root->next[i] = NULL; while(n--) { scanf("%s %s",instrc,str); if(strcmp(instrc ,"insert") == 0 && findT(str) == 0) Ctrie(str); else if(strcmp(instrc, "search") == 0) if(findT(str) == 1) printf("Yes\n"); else printf("No\n"); else if(strcmp(instrc,"delete") == 0) { if((i = findTk(str))) { find_del(str,i); } } } return 0; }
kill2020 | 2016-05-15 22:23:01# 1
一直过不了
一直过不了