Message Flood

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

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

这道题的大体意思就是说第一个数字表示她将要给发短信的人,第二数字表示给她发短信的人数;下面是人名。如果有人已经给他发了短信那么她就不要用给他回短信了;所以就是将前后人名进行比较,找到相同的人数,然后第一个数减去重复的人数就是需要发短信的人数。

这题就是一个前后的比较如果后面出现了就自减,所以用字典树进行查找;

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
    int flag;
    struct node *next[26];//这里通过结构体指针数组来确定26个方向;
} tree;
tree *newnode()
{
    tree *p = (tree*)malloc(sizeof(tree));//为p开辟空间,注意因为用了自定义所以是(sizeof(tree);
    for(int i = 0; i < 26; i++)
    {
        p -> next[i] = NULL;//让各个方向指空;
    }
    p -> flag = 0;//标记住此时节点是0;
    return p;
}
int find (tree *p1, char *s)//查找是否有相同的情况;
{
    tree *p = p1;
    for(int i = 0; s[i] != 0; p = p -> next[s[i] - 'a'], i++)
    {
        if(s[i] >= 'A' && s[i] <= 'Z')//因为存在大写的情况,所以要将大写的转化成小写的26个方向;
        {
            s[i] += 32;
        }
        if(p -> next[s[i]-'a'] == NULL)
        {
            return 0;//如果节点后面是空则返回0表示没有;
        }
    }
    if(p -> flag)//如果最后一个呗标记了,说明是相同的,所以返回1;
    {
        p -> flag = 0;//以为可以一个人给她发多条短信则,将标记去掉防止以后重复名字再是总人数减一;
        return 1;
    }
    return 0;
}
void free1(tree *p)//这是内存释放函数,防止内存过多超内存;
{
    if(p == NULL)
        return;
    for(int i = 0; i < 26; i++)
    {
        free1(p -> next[i]);//递归调用自己进行内存的释放;
    }
    free(p);
    return;
}
int main()
{
    tree *root, *p;
    int n,m,sum;
    char s[100];
    while(~scanf("%d",&n) && n != 0)
    {
        scanf("%d",&m);
        root = newnode();//建立第一个节点;
        sum = n;
        while(n--)
        {
            scanf("%s", s);
            p = root;
            for(int i = 0; s[i] != 0; p = p -> next[s[i]-'a'], i++)
            {
                if(s[i] >= 'A' && s[i] <= 'Z')
                    s[i] += 32;
                if(p -> next[s[i]-'a'] == NULL)
                    p -> next[s[i]-'a'] = newnode();//依次建立节点;
            }
            p -> flag = 1;//最后一个进行标记;
        }
        while(m--)
        {
            scanf("%s", s);
            if(find(root, s))//比较名字找出相同的;
                sum--;//总人数进行自减;
        }
        free1(root);
        printf("%d\n",sum);
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值