NOJ1121 Message Flood STL应用

题意

一共认识n个人,过节了,要给每个人都发一个短信。不过已经收到了m条短信,如果收到了某个认识的人的短信就不用再给他发短信了。最后要发多少短信呢?

思路

用STL的map能够简化问题。注意字符串不区分大小,所以用transform方法来把string全部大写化。

代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
int n,m;
int main()
{
    while(scanf("%d",&n) && n) {
        scanf("%d",&m);
        map<string,bool> mmap;
        for(int i = 0 ; i < n ; i ++) {
            string a;
            cin >> a;
            transform(a.begin(),a.end(),a.begin(),::toupper);
            mmap[a] = true;
        }
        int ans = 0;
        for(int i = 0 ; i < m ; i ++) {
            string a;
            cin >> a;
            transform(a.begin(),a.end(),a.begin(),::toupper);
            mmap[a] = false;
        }
        map<string,bool>::iterator it;
        for(it = mmap.begin() ; it != mmap.end() ; it ++) {
            if(it->second) ans ++;
        }
        cout << ans << endl;
    }
    return 0;
}

你可能感兴趣的:(算法与数据结构/ACM)