Facetook Priority Wall 题解

发布时间 2023-07-12 13:19:40作者: xvl

题目传送门

一道模拟题。

用一个 map 存储每个人的优先级因子,然后存进 vector 里进行排序。难点在于分辨 \(X\)\(Y\) 与当前是什么操作。

不过需要注意,只要出现了名字就需要输出,且我们认为与你没关系的人不加分。

Code

#include <bits/stdc++.h>
#define ll long long
#define INF 1e9
using namespace std;
string name;
int n, cnt;
map <string, int> mp;
vector <pair <string, int>> ans; 
bool cmp(const pair <string, int> &x, const pair <string, int> &y) { // 排序
    if (x.second != y.second) return x.second > y.second;
    return x.first < y.first; 
}
signed main() {
    ios :: sync_with_stdio(0);
    cin >> name >> n;
    cin.get(); // 接下来要 getline
    for (int i = 1; i <= n; i++) {
        int j;
        string s, x, y, op;
        getline(cin, s);
        for (j = s.size() - 8; s[j] != ' '; j--) y += s[j]; 
        reverse(y.begin(), y.end()); // 判断 Y
        for (j = 0; s[j] != ' '; j++) x += s[j]; // 判断 X
        for (j++; s[j] != ' '; j++) op += s[j]; // 判断是什么操作
        if (x != name and mp.find(x) == mp.end()) mp[x] = 0; 
        if (y != name and mp.find(y) == mp.end()) mp[y] = 0;
        // 只要出现了就要输出
        if (y == name) {
            if (op == "posted") mp[x] += 15;
            else if (op == "commented") mp[x] += 10;
            else mp[x] += 5;
        }
        if (x == name) {
            if (op == "posted") mp[y] += 15;
            else if (op == "commented") mp[y] += 10;
            else mp[y] += 5;
        }
    }
    for (auto v : mp) ans.push_back(make_pair(v.first, v.second));
    sort(ans.begin(), ans.end(), cmp);
    for (auto v : ans) cout << v.first  << "\n";
    return 0;
}