sdut 1500 Message Flood

一道简单的Trie树,好长时间没写过了,手生了,wA了好几次...

Message Flood

Time Limit:1500MS  Memory Limit:65536K
Total Submit:467 Accepted:93

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

题目大意:给n个字符串,然后又给出m个字符串,问前n个字符串有多少个没有在m个串中出现。每个字符串长度不超过10,不区分字母大小写。

数据量很大,简单模拟肯定不行,很显然要用Trie树。。

直接贴下代码:

View Code
 1 # include<stdio.h>
2 # include<string.h>
3 # define MAX 26
4 struct Trie{
5 int num;
6 struct Trie *next[MAX];
7 };
8 Trie *NewTrie()
9 {
10 int i;
11 Trie *p= new Trie;
12 for(i=0;i<MAX;i++)
13 p->next[i]=NULL;
14 p->num=0;
15 return p;
16 }
17 void Insert(Trie *p,char s[])
18 {
19 int i,len,ans;
20 len=strlen(s);
21 Trie *temp=p;
22 for(i=0;i<len;i++)
23 {
24 if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A';
25 else ans=s[i]-'a';
26 if(temp->next[ans]==NULL) temp->next[ans]=NewTrie();
27 temp=temp->next[ans];
28 }
29 temp->num=1;
30 }
31 int query(Trie *p,char s[])
32 {
33 Trie *temp=p;
34 int i,len,ans;
35 len=strlen(s);
36 for(i=0;i<len;i++)
37 {
38 if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A';
39 else ans=s[i]-'a';
40 if(temp->next[ans]==NULL) return 0;
41 temp=temp->next[ans];
42 }
43 if(temp->num==1) {temp->num=0;return 1;}
44 return 0;
45 }
46 void Delete(Trie *p)
47 {
48 int i;
49 for(i=0;i<MAX;i++)
50 if(p->next[i]!=NULL) Delete(p->next[i]);
51 delete p;
52 p=NULL;
53 }
54 int main()
55 {
56 int i,n,m,count;
57 char str[12];
58 Trie *p;
59 while(scanf("%d",&n)!=EOF && n)
60 {
61 scanf("%d",&m);
62 p=NewTrie();
63 for(i=0;i<n;i++)
64 {
65 scanf("%s",str);
66 Insert(p,str);
67 }
68 count=0;
69 while(m--)
70 {
71 scanf("%s",str);
72 count+=query(p,str);
73 }
74 printf("%d\n",n-count);
75 Delete(p);
76 }
77 return 0;
78 }

最后一个Delete函数,主要是为了释放空间, 不过时间耗时非常大。时间:500ms , 空间消耗1W+

去掉Delete函数,时间:250ms,空间消耗:5w+

你可能感兴趣的:(message)