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",n-sum);    

     Delete(T); 

 }

 return 0;

}  

 

 

posted @ 2014-06-23 21:13  人艰不拆_zmc  阅读(175)  评论(0编辑  收藏  举报