zoukankan      html  css  js  c++  java
  • 字典树

    Message Flood

    Time Limit: 1500ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

    Well, how do you feel about mobile phone? Your answer would probably be something like that "It's so convenient and benefits people a lot". However, If you ask Merlin this question on the New Year's Eve, he will definitely answer "What a trouble! I have to keep my fingers moving on the phone the whole night, because I have so many greeting message to send!" Yes, Merlin has such a long name list of his friends, and he would like to send a greeting message to each of them. What's worse, Merlin has another long name list of senders that have sent message to him, and he doesn't want to send another message to bother them Merlin is so polite that he always replies each message he receives immediately). So, before he begins to send message, he needs to figure to how many friends are left to be sent. Please write a program to help him. Here is something that you should note. First, Merlin's friend list is not ordered, and each name is alphabetic strings and case insensitive. These names are guaranteed to be not duplicated. Second, some senders may send more than one message to Merlin, therefore the sender list may be duplicated. Third, Merlin is known by so many people, that's why some message senders are even not included in his friend list.

    输入

    There are multiple test cases. In each case, at the first line there are two numbers n and m (1<=n,m<=20000), which is the number of friends and the number of messages he has received. And then there are n lines of alphabetic strings(the length of each will be less than 10), indicating the names of Merlin's friends, one per line. After that there are m lines of alphabetic strings, which are the names of message senders. The input is terminated by n=0.

    输出

    For each case, print one integer in one line which indicates the number of left friends he must send.

    示例输入

    5 3
    Inkfish
    Henry
    Carp
    Max
    Jericho
    Carp
    Max
    Carp
    0

    示例输出

    3
    法1:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    char str[20001][11],zancun[11];
    struct node{
        int flag;
        struct node *next[26];//通过结构体数组来指向26个方向
    };
    struct node *newnode(){
        int i;
        struct node *p=(struct node *)malloc(sizeof(struct node));此处不要忘了强制转化类型
        p->flag=0;标记此时的节点为0
        for(i=0;i<26;i++)
            p->next[i]=NULL;让26个方向都指空
        return p;
    }
    void insert(struct node *root,  char  *s)建立字典树,自身调用
    {
        struct node *p=root;
        int i,t,k=strlen(s);
        for(i=0;i<k;i++)
        {
            if(s[i]>='a'&&s[i]<='z')消除输入的字符是大写还是小写的影响
                t=s[i]-'a';
            else if(s[i]>='A'&&s[i]<='Z')
                t=s[i]-'A';
            if(p->next[t]==NULL)
                p->next[t]=newnode();以此建立节点	
            p=p->next[t];
        }
        p->flag=1;名字的最后一个字母进行标记
    }
    int search(struct node *root, char *s)//查找是否有相同的情况
    {
        int i,t,k=strlen(s);
        struct node *p=root;
        for(i=0;i<k;i++)
        {
            if(s[i]>='a'&&s[i]<='z')
                t=s[i]-'a';
            else if(s[i]>='A'&&s[i]<='Z')
                t=s[i]-'A';
            if(p->next[t]==NULL)
                return 0;如果节点后面为空,则返回0表示没有	
            p=p->next[t];
        }
        if(p->flag==1)如果最后一个标记,则其flag为1, 进入if语句
        {
            p->flag=0;因为一个人可以给她发多条短信,将标记去掉防止以后重复名字再使总人数减一 
            return 1;
        }
        return 0;
    }
    
    
    void Delete(struct node *root)//这是内存释放函数,防止内存过多超内存
    {
        int i;
    
        for(i = 0 ; i < 26 ; i++)
        {
            if(root->next[i] != NULL)
                Delete(root->next[i]);递归调用自身	
        }
    
        free(root);
    }
    int main()
    {
        int i,n,m,s;
        struct node *root;
        char b[100];
        while(scanf("%d",&n)!=EOF && n!=0)
        {
            scanf("%d",&m);
            s=n;
            root=newnode();建立第一个节点
            getchar();
            for(i=0;i<n;i++)输入n组字符串
            {
                scanf("%s",str[i]);
            }
            for(i=0;i<m;i++)
            {
                scanf("%s",b);
                insert (root,b);调用插入函数,建树
            }
            for(i=0;i<n;i++)
            {
    
                if(search(root,str[i]))如果search函数返回值为1,则执行if语句,s--;
                    s--;
            }
    
            printf("%d
    ",s);
            Delete(root);
        }
        return 0;
    }
    
    法2:
    #include <stdio.h>
    #include <string.h>
    struct node
    {
        int flag;
        int z[26];
    } num[1000001];
    char s[100];
    char ss[100];
    int ns=0;
    int newnode()
    {
        int p=ns;
        ns++;
        for(int i=0; i<26; i++)
        {
            num[p].z[i]=-1;
        }
        num[p].flag=0;
        return p;
    }
    void creat()
    {
        int i,k;
        scanf("%s",s);
        k=strlen(s);
        int qian;
        qian=0;
        int p;
        for(i=0; i<k; i++)
        {
            if(s[i]>='A'&&s[i]<='Z')s[i]=s[i]-'A'+'a';//A-a;
            int t=s[i]-'a';
            if(num[qian].z[t]==-1)
            {
                p=newnode();
                num[qian].z[t]=p;
            }
            else p=num[qian].z[t];
            qian=p;
        }
        num[p].flag=1;
    }
    int su=0;
    int len;
    int Search(int rt)
    {
        int j;
        int q=rt;
        for(j=0; j<len; j++)
        {
            if(ss[j]>='A'&&ss[j]<='Z')ss[j]=ss[j]-'A'+'a';//A-a;
            if(num[q].z[ss[j]-'a']==-1)return 0;
            q=num[q].z[ss[j]-'a'];
        }
        if(num[q].flag==1)
        {
            num[q].flag=0;
            return 1;
        }
        return 0;
    }
    int main()
    {
        int n,m;
        while(~scanf("%d",&n))
        {
            if(n==0)
                break;
            scanf("%d",&m);
            ns=0;
            int root=newnode();
            int nn=n;
            while(n--)
            {
                creat();
            }
            int sum=0;
            while(m--)
            {
                scanf("%s",ss);
                len=strlen(ss);
                sum+=Search(root);
              //  printf("%d
    ",sum);
            }
            printf("%d
    ",nn-sum);
        }
        return 0;
    }
    


     

    反思:第一次写的时候Memory Limit Exceeded, 可能很多人遇到这种情况,我开始建树的时候,把电话本中的朋友名单建成了树,其实这样会超内存,可以把给他发短信的同学的名字建成树,也就是说用电话本中的每一个朋友名字,到建的树中去匹配,如果匹配,s--即可,最后输出s。

    每天训练发现我比别人做的好慢,但是理解的更深刻,如果一开始学一个新知识点就搜模板,那么这样的人是走不远的,毕业之后带走的只有思维,什么荣誉,奖杯都已经不重要了。
  • 相关阅读:
    内置函数的补充
    python3 集合中的常用方法
    Salesforce: ISCHANGED在workflow中的使用
    Salesforce: setTreatTargetObjectAsRecipient的使用
    python实现用户登录次数太多账号"锁定"
    docker命令
    scrapy框架的安装
    分布式爬虫
    scrapy框架mongodb正规存储
    redis
  • 原文地址:https://www.cnblogs.com/6bing/p/3931302.html
Copyright © 2011-2022 走看看