SDUT1500_Message Flood(字典树)

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

来源

第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 1500
	User name	: acm2013叶思俊
	Result		: Time Limit Exceeded
	Take Memory	: 0K
	Take Time	: 1510MS
	Submit 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);
    }
}



SDUT-OJ(Software Development University of Tsinghua Online Judge)是一个在线编程平台,提供给清华大学软件学院的学生和爱好者练习和解决算法问题的环境,其中包括各种计算机科学题目,包括数据结构、算法、图形等。对于"最小生成树"(Minimum Spanning Tree, MST)问题,它是图论中的经典问题,目标是从一个加权无向图中找到一棵包含所有顶点的树,使得树的所有边的权重之和最小。 在C语言中,最常见的是使用Prim算法或Kruskal算法来求解最小生成树。Prim算法从一个顶点开始,逐步添加与当前生成树相连且权重最小的边,直到所有顶点都被包含;而Kruskal算法则是从小到大对所有边排序,每次选取没有形成环的新边加入到树中。 如果你想了解如何用C语言实现这些算法,这里简单概括一下: - 通常使用优先队列(堆)来存储边和它们的权重,以便快速查找最小值。 - 从任意一个顶点开始,遍历与其相邻的边,若新边不形成环,就更新树,并将新边加入优先队列。 - Kruskal算法: - 先将所有的边按照权重升序排序。 - 创建一个空的最小生成树,然后依次取出排序后的边,如果这条边连接的两个顶点不在同一个连通分量,则将其添加到树中。 如果你需要更详细的代码示例,或者有具体的问题想了解(比如如何处理环、如何实现优先队列等),请告诉我,我会为你提供相应的帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值