zoukankan      html  css  js  c++  java
  • oj1500(Message Flood)字典树

    大意:输入几个字符串,然后再输入几个字符串,看第一次输入的字符串有多少没有在后面的字符串中出现(后输入的字符串不一定出现在之前的字符串中)

    #include <stdio.h>

    #include <string.h>

    #include <stdlib.h>

    typedef struct Node

    {    

      int flag;

         struct Node *next[26];

    }Node,*Tree;

    char a[20010][20];

    int n,m;

    void Creat(Tree &T)

    {   

          int i;  

         T=(Tree)malloc(sizeof(Node));

         T->flag=0;

         for(i=0;i<26;i++)   

          T->next[i]=NULL;

    }

    void insert(Tree &T,char *s)

    {   

      int l,i,t;   

      Tree p=T;   

      l=strlen(s);   

      for(i=0;i<l;i++)   

      {     

        if(s[i]>='a'&&s[i]<='z')   

        t=s[i]-'a';  

        else t=s[i]-'A';

         if(p->next[t]==NULL)   

        Creat(p->next[t]);    

          p=p->next[t];   

        }  

        p->flag=1;

    }

    int search(Tree T,char *s)

    {    

      Tree p=T;

        int i,k,t;

         k=strlen(s);

         for(i=0;i<k;i++) 

        {      

            if(s[i]>='A'&&s[i]<='Z')    

              t=s[i]-'A';        

           else            

        t=s[i]-'a'; 

        if(p->next[t]==NULL)  

          return 0;   

       p=p->next[t];

     }

     if(p->flag)

     {    

       return 1;

     }

       else return 0;

    }

    void Delete(Node *p) 

    {    

        int i; 

        for(i=0; i<26; i++) 

        {        

        if(p->next[i]!=NULL)      

            Delete(p->next[i]); 

        }    

        free(p); 

      } 

      int main()

    {    

       int i,j,sum;

      char str[20];

      Tree T;  

    while(scanf("%d",&n)!=EOF&&n!=0)

     {       

      Creat(T);    

       sum=0;   

        scanf("%d",&m);

        for(i=0;i<n;i++)  

        scanf("%s",a[i]);  

       for(i=1;i<=m;i++)  

       {    

         scanf("%s",str);  

         insert(T,str);    

      }    

    for(i=0;i<n;i++)

       {      

      j=search(T,a[i]);   

         if(j)   

       sum++;    

    }    

    printf("%d ",n-sum);    

         Delete(T); 

     }

     return 0;

    }  

  • 相关阅读:
    python列表--查找集合中重复元素的个数
    python3-打印一个进度条
    python3-sys模块
    python3-字符串操作
    python3-深浅复制
    python3-os模块
    接口和抽象类有什么区别
    集合知识
    面向对象的特征有哪些方面
    javadoc时候乱码-编码 GBK 的不可映射字符
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/3804960.html
Copyright © 2011-2022 走看看