Message Flood

来源:互联网 发布:淘宝有没有分期付款的 编辑:程序博客网 时间:2024/04/29 10:10

Message Flood

Time Limit: 1500ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

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.

输入

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.

输出

For each case, print one integer in one line which indicates the number of left friends he must send.

示例输入

5 3InkfishHenryCarpMaxJerichoCarpMaxCarp0

示例输出

3

来源

第9届中山大学程序设计竞赛预选赛

利用map:

<pre name="code" class="cpp">#include<bits/stdc++.h>using namespace std;int main(){     int n,m,i,j;     char s[30];     map<string,int> mp;     while (cin>>n,n!=0)     {           mp.clear();          cin>>m;          for (i=0;i<n;i++)          {                cin>>s;                for (j=0;j<strlen(s);j++)                s[j]=tolower(s[j]);                mp[s]++;          }          for (i=0;i<m;i++)          {             cin>>s;             for (j=0;j<strlen(s);j++)              s[j]=tolower(s[j]);              if (mp.count(s))               mp.erase(s);          }          cout<<mp.size()<<endl;     }     return 0;}




利用set:

<pre name="code" class="cpp">#include<bits/stdc++.h>using namespace std;int main(){        int n,m,i,j;        char s[100];        set<string> mp;        while (cin>>n,n!=0)        {             cin>>m;             mp.clear();             for (i=0;i<n;i++)             {                   cin>>s;                   for (j=0;j<strlen(s);j++)                   s[j]=tolower(s[j]);                   mp.insert(s);             }             for (i=0;i<m;i++)             {                  cin>>s;                  for (j=0;j<strlen(s);j++)                  s[j]=tolower(s[j]);                  if (mp.count(s))                  mp.erase(s);             }             cout<<mp.size()<<endl;        }         return 0;}




//某牛人做
       这题本来很容易,由于自己看漏了个条件(大小写不敏感)WA了无数,还有用错了stl里的sort(参数传错了)

       用stl里的sort排数组时, sort(begin, end, cmp),第二个参数要是数组最后元素的后一个的指针,当一个数组a有n个元素时,end= a+n(&a[n]),而不是&a[n-1]

 
#include <iostream>
#include <string>
#include <algorithm>//algorithm,用cmp和sort函数时要添加的头文件
#include <memory.h>
//用menset时要添加的头文件
using namespace std;
  
bool cmp(string a, string b)
{
    return a < b;   //这个是增序
}
  
string name[20001], tmp;   //都是全局变量了
bool found[20001];
int n, m;
bool bi_search();
  
int main()
{
    int count;
  
    while (cin >> n && n)        //这句话特别有用,要会使用
    {
        cin >> m;
        memset(found, false, 20001*sizeof(bool));                // 没有了这句话会有wrong answer
        for (int i = 0; i < n; i++)
        {
            cin >> name[i];
            for (int j = 0; j < name[i].length(); j++)
                name[i][j] = tolower(name[i][j]);                                //将大写字母转化成小写字母
        }
        count = n;
        sort(name, name+n, cmp);                //注意使用方法
        for (int i = 0; i < m; i++)
        {
            cin >> tmp;
            for (int j = 0; j < tmp.length(); j++)
                tmp[j] = tolower(tmp[j]);
  
            if (bi_search())
                count--;
        }
        cout << count << endl;
    }
    return 0;
}

 

//这个二分法写得很好,适当记下  

bool bi_search()
{
    int start = 0, mid, end = n-1;
    while (start <= end)
    {
        mid = (start+end)/2;
  
        if (tmp == name[mid])
        {
            if (!found[mid])
            {
                found[mid] = true;
                return true;
            }
            else
                return false;
        }
        else if (tmp < name[mid])
            end = mid-1;
        else 
            start = mid+1;
    }
    return false;
}



map的基本操作函数: 
      C++ Maps是一种关联式容器,包含“关键字/值”对   
      begin()         返回指向map头部的迭代器   
      clear()        删除所有元素 
      count()         返回指定元素出现的次数  
      empty()         如果map为空则返回true   
      end()           返回指向map末尾的迭代器  
     equal_range()   返回特殊条目的迭代器对     
      erase()         删除一个元素   
      find()          查找一个元素    
     get_allocator() 返回map的配置器    
     insert()        插入元素 
     key_comp()      返回比较元素key的函数 
     lower_bound()   返回键值>=给定元素的第一个位置     
      max_size()      返回可以容纳的最大元素个数   
      rbegin()        返回一个指向map尾部的逆向迭代器    
      rend()          返回一个指向map头部的逆向迭代器   
      size()          返回map中元素的个数    
      swap()           交换两个map 
     upper_bound()    返回键值>给定元素的第一个位置  
      value_comp()     返回比较元素value的函数

0 0