字典树

题目:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=690&pid=1003

#include <stdio.h>
#include <string.h>
struct node{
    int num;
    node *next[30];
    node(){
        memset(next,0,sizeof(next));
        num=0;
    }
};
node *root=new node;
node *p;
char dir[32],s[32];
void Insert(){
    p=root;
    for(int i=0;s[i];i++)
    {
        int x=s[i]-'a';
        if(p->next[x]==NULL)    p->next[x]=new node;
        p=p->next[x];
        p->num++;
    }
}
int Search(){
    p=root;
    for(int i=0;s[i];i++){
        int x=s[i]-'a';
        if(p->next[x]==NULL)
        return 0;
        p=p->next[x];
    }
    return p->num;
}
void Delete(int cnt){
    p=root;
    for(int i=0;s[i];i++)
    {
        int x=s[i]-'a';
        p=p->next[x];
        p->num-=cnt;
    }
    for(int i=0;i<30;i++)   p->next[i]=0;
}

int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s %s",dir,s);
        if(dir[0]=='i') Insert();
        else if(dir[0]=='s')
        {
            int ans=Search();
            if(ans )    printf("Yes\n");
            else        printf("No\n");
        }
        else
        {
            int cnt=Search();
            if(cnt) Delete(cnt);
        }
    }
    return 0;
}

题目:http://acm.hdu.edu.cn/search.php?action=listproblem

参考:http://blog.csdn.net/cambridgeacm/article/details/7752247

#include<cstdio>
 2 #include<cstring>    //memset函数的头文件
 3 #include<iostream>
 4 using namespace std;
 5 struct node{
 6      int count;
 7      node *next[26]; 
 8      node(){  //初始化数据 
 9          memset(next,NULL,sizeof(next));
10          count=0;
11      }
12 };
13 node *p,*root=new node();
14 void insert(char *s)//插入新单词,即建立字典树
15 {
16      int i,k;
17      for(p=root,i=0;s[i];++i)
18      {
19          k=s[i]-'a';
20          if(p->next[k]==NULL) p->next[k]=new node();//判断是不是新节点,如果是分配创建一个新节点来存贮 ,即root的next域对应的k位置是否为空 
21          p=p->next[k];
22          p->count++;  //记录此字母出现的次数 
23      }
24 }
25 int search(char *s)  //寻找函数 
26 {
27      int i,k;
28      for(p=root,i=0;s[i];++i)
29      {
30          k=s[i]-'a';
31          if(p->next[k]==NULL) break; //一旦查找不到,立即跳出 
32          p=p->next[k];
33      }
34      if(s[i]) return 0;//s[i]!=0表示中间 
35      return p->count;  //返回出现的次数
36 }
37 int main()
38 {
39      char s[11];
40      while(gets(s),*s) insert(s); //一直读入数据,直到遇到空字符串 
41      while(gets(s))
42          printf("%d\n",search(s));
43      return 0;
44 }

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1671

#include <iostream>  
#include <cstdio>  
#include <cstring>  
using namespace std;  
struct node  
{  
    int count;  
    node *next[10];  
    node():count(0){memset(next,0,sizeof(next));}  
};  
node *root;  
node *b[10003];  
int k=0;  
void insert(char *a)  
{  
    int l=strlen(a);  
    node *p=root;  
    int i;  
    for(i=0;i<l;i++)  
    {  
        if(p->next[a[i]-'0']==0)  
        {  
            p->next[a[i]-'0']=new node;  
        }  
        p=p->next[a[i]-'0'];  
        p->count++;  
    }  
    b[k++]=p;  
}  
int check(int n)  
{  
    int i;  
    for(i=0;i<k;i++)  
    {  
        if(b[i]->count!=1)  
            return 1;  
    }  
    return 0;  
}  
void de(node *p)  
{  
    if(p==0)  
        return ;  
    int i;  
    for(i=0;i<10;i++)  
    {  
        de(p->next[i]);  
    }  
    delete p;  
  
}  
int main()  
{  
    int t;  
    scanf("%d",&t);  
    char a[15];  
    while(t--)  
    {  
        root = new node;  
        int n;  
        k=0;  
        scanf("%d",&n);  
        int i;  
        for(i=0;i<n;i++)  
        {  
            scanf("%s",a);  
            insert(a);  
        }  
        if(check(n))  
            printf("NO\n");  
        else  
            printf("YES\n");  
        de(root);  
    }  
    return 0;  
}  
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4287

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2846


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值