sicily 1194 Message Flood (STL的map和set应用练习)

题目:

D - 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

STL真心方便啊。这题分别用了map和set做了一下。注意这个题有个坑点,就是不区分大小写。。。。。

map代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>

using namespace std;
int main()
{
    map<string,int>mp;
    map<string,int>::iterator iter;
    int n, m, i, len, j;
    char s[30];
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d",&m);
        getchar();
        mp.clear();//如果在外面定义的话要注意清空。
        for(i=0;i<n;i++)
        {
            gets(s);
            len=strlen(s);
            for(j=0;j<len;j++)
            {
                s[j]=tolower(s[j]);//tolower函数的意义是将大写字母全换成小写字母
            }
            mp[s]++;
        }
        for(i=0;i<m;i++)
        {
            gets(s);
            len=strlen(s);
            for(j=0;j<len;j++)
            {
                s[j]=tolower(s[j]);
            }
            mp.erase(s);//erase函数的意义是在该容器中删除该元素
        }
        printf("%d\n",mp.size());//直接用size函数输出容器内元素的个数,不用遍历计数。
    }
    return 0;
}

set代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include<algorithm>

using namespace std;
int main()
{
    int n, m, i, len, j;
    char s[30];
    while(scanf("%d",&n)!=EOF&&n)
    {
        scanf("%d",&m);
        getchar();
        set<string>mp;//声明set容器
        set<string>::iterator iter;//声明set迭代器
        for(i=0; i<n; i++)
        {
            gets(s);
            len=strlen(s);
            for(j=0; j<len; j++)
            {
                s[j]=tolower(s[j]);
            }
            mp.insert(s);//向set里加入一个元素
        }
        for(i=0; i<m; i++)
        {
            gets(s);
            len=strlen(s);
            for(j=0; j<len; j++)
            {
                s[j]=tolower(s[j]);
            }
            if(mp.count(s))//判断容器里是否存在该元素
                mp.erase(s);//如果容器里存在该元素,则删除
        }
        printf("%d\n",mp.size());//直接输出容器剩余元素个数。
    }
    return 0;
}

sicily 1194 Message Flood (STL的map和set应用练习)

时间: 2024-07-28 12:43:04

sicily 1194 Message Flood (STL的map和set应用练习)的相关文章

Sicily 1194. Message Flood

题目地址:1194. Message Flood 思路: 不区分大小写,先全部转化为小写,用stl提供的函数做会很方便. 具体代码如下: 1 #include <iostream> 2 #include <set> 3 #include <string> 4 using namespace std; 5 6 int main() { 7 int n, m; 8 while (cin >> n && n) { 9 cin >> m;

NOJ1121 Message Flood STL应用

题意 一共认识n个人,过节了,要给每个人都发一个短信.不过已经收到了m条短信,如果收到了某个认识的人的短信就不用再给他发短信了.最后要发多少短信呢? 思路 用STL的map能够简化问题.注意字符串不区分大小,所以用transform方法来把string全部大写化. 代码 #include <cstdio> #include <cstring> #include <map> #include <algorithm> #include <string>

STL 之map解决 Message Flood(原字典树问题)

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".

NOJ 1121 Message Flood (Trie树 或者 map)

Message Flood 时间限制(普通/Java):2000MS/6000MS         运行内存限制:65536KByte 总提交:399          测试通过:105 题目描述 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". Howeve

ZOJ2724_Windows Message Queue(STL/优先队列)

解题报告 题意: 看输入输出就很明白. 思路: 优先队列. #include <algorithm> #include <iostream> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <cstdio> #include <map> using namespace std; struct node

C++ STL中Map的按Key排序和按Value排序

原文  http://blog.csdn.net/iicy266/article/details/11906189 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义,map<string, int>,其中学生姓名用string类型,作为Key:该学生的成绩用int类型,作为value.这样一来,我们可以根据学

HDU Bombing (STL multiset+map)

题意:给你 n 个坐标(x,y),m 个询问(c,d) c==0,求出x==d有多少个,并删除这些点: c==1,求出y==d有多少个,并删除这些点. map+multiset的多重映射 #include<cstdio> #include<stdlib.h> #include<string.h> #include<string> #include<map> #include<cmath> #include<iostream>

STL中map与hash_map容器的选择

[转]STL中map与hash_map容器的选择 先看看alvin_lee 朋友做的解析,我觉得还是很正确的,从算法角度阐述了他们之间的问题! 实际上这个问题不光C++会遇到,其他所有语言的标准容器的实现及选择上都是要考虑的.做应用程序你可能觉得影响不大,但是写算法或者核心代码就要小心了.今天改进代码,顺便又来温习基础功课了. 还记得Herb Sutter那极有味道的<C++对话系列>么,在其中<产生真正的hash对象>这个故事里就讲了map的选择.顺便回顾一下,也讲一下我在实用中

STL中map与hash_map的比较

1. map : C++的STL中map是使用树来做查找算法; 时间复杂度:O(log2N) 2. hash_map : 使用hash表来排列配对,hash表是使用关键字来计算表位置; 时间复杂度:O(1), 最坏的时间复杂度:O(n) 总体来说:hash_map 比 map 查找速度快,而且查找速度基本和数据量大小无关,属于常数级别,节省一定内存,如果没有必要排序的话,尽量使用 hash_map . 注:hash还有hash函数的耗时.当有100w条记录的时候,map也只需要20次的比较,20