SDUT1500_Message Flood(字典树)

来源:互联网 发布:主角在美国淘宝的小说 编辑:程序博客网 时间:2024/06/01 09:44

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 3InkfishHenryCarpMaxJerichoCarpMaxCarp0

示例输出

3

来源

第9届中山大学程序设计竞赛预选赛

解题报告
这题在当初学字符串的时候做的,又是英文题,题意大体是一个人手上有一个名单,在新年那一天他需要给名单上的人发短信,然而有小伙伴先给他发短信了(可以发多次信息),他一收到短信就回复小伙伴,问题是除去给他发信息的小伙伴,他还需要给几个人发信息。
一开始就以为是模拟的题目,直接敲代码
#include<stdio.h>#include<string.h>#include<stdlib.h>int cmp(const void *p1,const void *p2)//对于char类型的排序{    //降序排序    //return strcmp((char *)p2,(char *)p1);    //升序排序    return strcmp((char *)p1,(char *)p2);}char name[20020][100],send[20020][100];int main(){    int n,m;    int i,j;    while(scanf("%d",&n)!=EOF&&n)    {        scanf("%d%*c",&m);        int c=0,l=0;        for(i=0;i<n;i++)        {            scanf("%s",name[i]);        }        for(i=0;i<m;i++)        {            scanf("%s",send[i]);        }        qsort(send,m,sizeof(send[0]),cmp);        j=0;        for(i=0;i<m;i++)        {            if(strcmp(send[i],send[i+1])!=0)            {                strcpy(send[j++],send[i]);                l++;            }        }        for(i=0;i<l;i++)            for(j=0;j<n;j++)            {                if(strcmp(name[j],send[i])==0)                    c++;            }        printf("%d\n",n-c);    }}/**************************************Problem id: SDUT OJ 1500User name: acm2013叶思俊Result: Time Limit ExceededTake Memory: 0KTake Time: 1510MSSubmit Time: 2014-02-10 15:12:17**************************************/
这题给的时间是1500ms,两个名单都是20000- 的,双重循环都4亿次的节奏,还用到快排。。。超时。。。
还用了链表写了次。。。
#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{    char name[100];    node *next;};int main(){    int n,m;    int i,j;    while(scanf("%d",&n)!=EOF&&n)    {        int c=0;        scanf("%d%*c",&m);        node *head1=new node;        head1->next=NULL;        node *head2=new node;        head2->next=NULL;        node *tail1=head1,*tail2=head2;        for(i=0;i<n;i++)        {            node *p=new node;            scanf("%s",p->name);            p->next=NULL;            tail1->next=p;            tail1=p;        }        for(i=0;i<m;i++)        {            node *p=new node;            scanf("%s",p->name);            p->next=NULL;            tail2->next=p;            tail2=p;        }        node *q=head2->next;        while(q!=NULL)        {            node *p=head1;            while(p->next!=NULL)            {                if(strcmp(q->name,p->next->name)==0)                {                    p->next=p->next->next;                    c++;                }                else                {                    p=p->next;                }            }            q=q->next;        }        printf("%d\n",n-c);    }}

同样超时。。。今天学了字典树,是个好东西。。。
要注意的是以哪个名单建字典树。。。
以名单一的建树,你对名单二查找时还要注意重复名字的。。。
所以以名单二建字典树。。。
#include <stdio.h>#include <string.h>#include <algorithm>struct node{    int v;    node *next[26];};node *newnode(){    node *p=new node;    p->v=0;    for(int i=0;i<26;i++)    {        p->next[i]=NULL;    }    return p;}void insertnode(node *root,char *str){    node *p=root;    int l=strlen(str);    for(int i=0;i<l;i++)    {        int t;        if(str[i]>='a')            t=str[i]-'a';        else t=str[i]-'A';        if(p->next[t]==NULL)        {            p->next[t]=newnode();        }        p=p->next[t];    }    p->v=1;}int find(node *root,char *str){    node *p=root;    int l=strlen(str);    for(int i=0;i<l;i++)    {        int t;        if(str[i]>='a')            t=str[i]-'a';        else t=str[i]-'A';        if(p->next[t]==NULL)            return 0;        else p=p->next[t];    }    if(p->v==1)        return 1;    return 0;}int main(){    int n,m;    int i,j,k;    char name[30000][15],send[100000];    while(~scanf("%d",&n)&&n)    {        int c=0;        node *root=newnode();        scanf("%d%*c",&m);        for(i=0;i<n;i++)        {            scanf("%s",name[i]);        }        for(i=0;i<m;i++)        {            scanf("%s",send);            insertnode(root,send);        }        for(i=0;i<n;i++)        {            if(!find(root,name[i]))                c++;        }        printf("%d\n",c);    }}



0 0
原创粉丝点击