A - Message Flood(字典树)


Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

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.

Input

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.

Output

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

Sample Input

5 3
Inkfish
Henry
Carp
Max
Jericho
Carp
Max
Carp
0

Sample Output

3
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
using namespace std;
//建立一个结构体
struct node
{
    int flag;//设置一个标记
    struct node *next[26];
};
//建树
struct node *creat()
{
    struct node *p;
    p=(struct node *)malloc(sizeof(struct node ));//申请内存
    for(int i=0;i<26;i++)//循环遍历26个字母
    {
        p->next[i]=NULL;//设为空
    }
    p->flag=0;//表示没有被标记
    return p;
}
//插入操作
void Insert(struct node *p,char *s)
{
    int i,ans,len;
    len=strlen(s);
    for(i=0;i<len;i++)//循环遍历整个字符串
    {
        if(s[i]>='A' && s[i]<='Z')
            ans=s[i]-'A';
        else
            ans=s[i]-'a';
        if(p->next[ans]==NULL)
            p->next[ans]=creat();//重新建树
        p=p->next[ans];
    }
    p->flag=1;//设置为已经标记
}
//查找操作
int Find(struct node *p,char *s)
{
    int i,ans,len;
    len=strlen(s);//测出字符串长度
    for(i=0;i<len;i++)//循环遍历整个字符串
    {
        if(s[i]>='A' && s[i]<='Z')
            ans=s[i]-'A';
        else
            ans=s[i]-'a';
        if(p->next[ans]==NULL)
            return 0;
        p=p->next[ans];
    }
    if(p->flag==1)
    {
        p->flag=0;
        return 1;
    }
    return 0;
}
//释放内存
void Delete(struct node *p)
{
    int i;
    for(i=0;i<26;i++)
    {
        if(p->next[i]!=NULL)
            Delete(p->next[i]);
    }
    free(p);//释放
}

int main()
{
    int n,m,i,num;
    char s[20000][100],s1[100];
    struct node *tree;
    while(scanf("%d %d",&n,&m),n)//输入操作
    {
        tree=creat();
        for(i=0;i<n;i++)
        {
            scanf("%s",s[i]);
        }
        num=0;
        while(m--)
        {
            scanf("%s",s1);
            Insert(tree,s1);
        }
        for(i=0;i<n;i++)
        {
            num+=Find(tree,s[i]);
        }
        printf("%d\n",n-num);
        Delete(tree);
    }
    return 0;
}
 
#include<iostream>//这是用的函数
#include<stdio.h>
#include<string.h>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);//加快输入的速度
    int n,m;
    string x;
    while(cin>>n&&n)//输入n并且n不等于0;
    {
        cin>>m;
        set<string>T;
        while(n--)
        {
            cin>>x;
            for(int i=0;i<=x.size();i++)
                x[i]=tolower(x[i]);//把字符都转化成小写的字符
            T.insert(x);//把x装入容器
        }
        while(m--)
        {
            cin>>x;
            for(int i=0;i<=x.size();i++)
                x[i]=tolower(x[i]);
                if(T.count(x))//如果x在容器中值为1
                    T.erase(x);//如果不在删除

        }
        cout<<T.size()<<endl;
    }
    return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章