L1-020 帅到没朋友

题解

       朴素做法就是一一比对,时间复杂度为$O(N * K * M)$,而且写起来十分吃力,本人开始就是这么做的,还卡了两个测试点。

       而这次介绍的解法的时间复杂度为$O(N * K)$,而且写起来十分简单, 采用 map <string, int> 来表示该人是否帅到没朋友,而这类好处就是后期可以直接判断并修改 map ,以达到判断但不重复输出的好处。

       核心点在于朋友圈中人的个数以判断该朋友圈里面的人是否帅到没朋友。

#include <iostream>
#include <cstring>
#include <map>
using namespace std;

int n, m, k;

int main()
{ 
    map <string, int> mp;
    cin >> n;
    while (n--) {
        cin >> k;
        string s; 
        for (int i = 0; i < k; ++i) {
            cin >> s;
            if (k > 1) {
                mp[s] = 1;
            }
        }
    }
    cin >> m; 
    int t = 0;
    bool flag = false;
    while (m--) {
        string s;
        cin >> s;
        if (mp[s] >= 1) {
            continue;
        }
        if (!t) t = 1;
        else cout << " ";
        ++mp[s];
        cout << s;
        flag = true;
    }
    if (!flag) {
        cout << "No one is handsome";
    }
    cout << endl;
    return 0;
}

 

posted @ 2020-10-30 15:40  Fool_one  阅读(149)  评论(0编辑  收藏  举报