随笔 - 6, 文章 - 0, 评论 - 24, 引用 - 0
数据加载中……

Trie在程序设计竞赛中的应用

Trie在程序设计竞赛中的应用

题目

 Message Flood
Problem 

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 messages 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 messages, 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


代码

 1#include <iostream>
 2#include <string>
 3#include "trie.h"
 4
 5struct Index {
 6    int operator[](char ch) {
 7        if (isupper(ch)) return ch - 'A';
 8        return ch - 'a';
 9    }

10}
;
11
12int n, m;
13std::string name;
14trie<26, Index> name_set;
15
16int main() {
17    while (std::cin>>n>>&& n) {
18        name_set.clear();
19        for (int i = 0; i < n; ++i) {
20            std::cin>>name;
21            name_set.insert(name.begin(), name.end());
22        }

23        int count = 0;
24        while (m--{
25            std::cin>>name;
26            count += name_set.erase(name.begin(), name.end());
27        }

28        printf("%d\n", n - count);
29    }

30}

31


测试数据

输入数据
输出数据

参考资料
郭嵩山、张子臻、王磊、汤振东著  国际大学生程序设计竞赛例题解(五)  电子工业出版社

posted on 2009-03-28 11:45 yuyang7 阅读(1587) 评论(0)  编辑 收藏 引用 所属分类: 程序设计竞赛


只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理