HDU-5687-Problem C

#include"bits/stdc++.h"
using namespace std;
struct tree{
    tree* Next[26];
    int cnt;
}*root;
tree* init(){
    tree* t=(tree*)malloc(sizeof(tree));
    memset(t->Next,NULL,sizeof(t->Next));
    t->cnt=0;
    return t;
}
void in(char* s){
    tree* now=root;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(!now->Next[j])now->Next[j]=init();
        now=now->Next[j];
        now->cnt++;
    }
}
void out(char* s){
    tree* now=root;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(!now->Next[j]){
            puts("No");
            return;
        }
        now=now->Next[j];
    }
    puts("Yes");
}
void era(tree* t){
    for(int i=0;i<26;i++)
    if(t->Next[i])era(t->Next[i]);
    free(t);
}
void de(char* s){
    tree* now=root;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(!now->Next[j])return;
        now=now->Next[j];
    }
    int n=now->cnt;now=root;
    for(int i=0;s[i];i++){
        int j=s[i]-'a';
        if(now->Next[j]->cnt==n){
            era(now->Next[j]);
            now->Next[j]=NULL;
            return;
        }
        now=now->Next[j];
        now->cnt-=n;
    }
}
char s1[10],s2[35];
int main(){
    int n;root=init();
    scanf("%d",&n);
    while(n--){
        scanf("%s%s",s1,s2);
        if(s1[0]=='i')in(s2);
        else if(s1[0]=='s')out(s2);
        else de(s2);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Angel-Demon/p/9738653.html