zoukankan      html  css  js  c++  java
  • 字典树 sdut acm 1500 Message Flood

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1500&cid=1147

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include <stdlib.h>
     4 
     5 struct node
     6 {
     7     int flag;
     8     struct node * next[26];
     9 };
    10 
    11 struct node * newnode()
    12 {
    13     int i;
    14     struct node * p =(struct node * )malloc(sizeof(struct node));
    15                               p->flag = 0;
    16                               for(i = 0; i < 26; i++)
    17                               p->next[i] = NULL;
    18                               return p;
    19 }
    20 
    21 void insert(struct node * root, char *s)
    22 {
    23     struct node * p = root;
    24     int i, len = strlen(s), t;
    25     for(i = 0; i < len; i++)
    26     {
    27         if(s[i]>='A' && s[i] <= 'Z') t = s[i] -'A';
    28         else t = s[i] - 'a';
    29         if(p->next[t] == NULL)
    30             p->next[t] = newnode();
    31         p = p->next[t];
    32     }
    33     p->flag = 1;
    34 }
    35 
    36 int search(struct node * root, char *s)
    37 {
    38     struct node * p = root;
    39     int i, t, len = strlen(s);
    40     for(i = 0; i < len; i++)
    41     {
    42         if(s[i]>='A' && s[i] <= 'Z') t = s[i] -'A';
    43         else t = s[i] - 'a';
    44         if(p->next[t] == NULL)
    45             return 0;
    46         p = p->next[t];
    47     }
    48     if(p->flag)
    49         {p->flag = 0; return 1;}
    50     return 0;
    51 }
    52 
    53 void ReleaseTrie(struct node *T){
    54     int i;
    55     for(i=0; i<26; i++){
    56         if(T->next[i]) ReleaseTrie(T->next[i]);
    57         T->next[i] = NULL;
    58     }
    59     free(T);
    60 }
    61 
    62 int main()
    63 {
    64     int n,m,t,i;
    65     char s[20];
    66     while(scanf("%d",&n)!=EOF&&n!=0)
    67     {
    68         struct node *root=NULL;
    69         root=newnode();
    70         scanf("%d",&m);
    71         for(i=1;i<=n;i++)
    72         {
    73             scanf("%s",s);
    74             insert(root,s);
    75         }
    76         while(m--)
    77         {
    78             scanf("%s",s);
    79             t=search(root,s);
    80             if(t==1)
    81             n--;
    82         }
    83         printf("%d\n",n);
    84         ReleaseTrie(root);
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    Django基础(一)
    CSS
    HTML
    python之路_面向对象
    python之路第六篇
    python之路第四篇
    python之路第三篇
    python之路第二篇
    python之路第一篇
    hdu 3551(一般图的匹配)
  • 原文地址:https://www.cnblogs.com/bfshm/p/3006154.html
Copyright © 2011-2022 走看看