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;
}



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