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個串中出現(xiàn)。每個字符串長度不超過10,不區(qū)分字母大小寫。
數據量很大,簡單模擬肯定不行,很顯然要用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+
轉載于:https://www.cnblogs.com/183zyz/archive/2011/08/04/2127868.html
總結
以上是生活随笔為你收集整理的sdut 1500 Message Flood的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Arcengine 10 ecp
- 下一篇: OSPF DR选举的先后