2021 Robocom 决赛 第三题

原题链接:

PTA | 程序设计类实验辅助教学平台

题面:

拼题 A 系统为提高用户账户的安全性,打算开发一个自动安全预警的功能。对每个账户的每次登录,系统会记录其登录的 IP 地址。每隔一段时间,系统将统计每个账户从多少不同的 IP 地址分别登录了多少次。如果某个账户的登录 IP 超过了 TIP​ 种,并且登录过于频繁,超过了 Tlogin​ 次,则会自动向管理员发出警报。

下面就请你实现这个预警功能。

输入格式:

输入首先在第一行中给出三个正整数:N(≤104)为登录记录的条数;TIP​ 和
Tlogin​,定义如题面中所描述,均不超过 100。

随后 N 行,每行格式为:

账户邮箱 IP地址

其中 账户邮箱 为长度不超过 40 的、不包含空格的非空字符串;IP地址 为形如 xxx.xxx.xxx.xxx 的合法 IP 地址。

输出格式:

按照登录所用不同 IP 的数量的非递增顺序,输出每个预警账户的信息。格式为:

账户邮箱
IP1 登录次数
IP2 登录次数
……

其中 IP 按登录次数的非递增顺序输出,如有并列,则按 IP 的递增字典序输出。此外,对所用不同 IP 的数量并列的用户,按其账户邮箱的递增字典序输出。

另一方面,即使没有账户达到预警线,也输出登录所用不同 IP 的数量最多的一批账户的信息。

输入样例 1:

24 3 4
[email protected] 218.109.231.189
[email protected] 112.192.203.187
[email protected] 112.18.235.143
[email protected] 112.192.203.187
[email protected] 113.18.235.143
[email protected] 111.192.203.187
[email protected] 218.109.231.189
[email protected] 111.18.235.143
[email protected] 115.192.203.187
[email protected] 113.189.58.141
[email protected] 111.192.203.187
[email protected] 112.18.58.145
[email protected] 114.192.203.187
[email protected] 112.18.235.143
[email protected] 123.89.158.214
[email protected] 112.18.235.143
[email protected] 218.109.231.189
[email protected] 113.192.203.187
[email protected] 218.109.231.189
[email protected] 114.192.203.187
[email protected] 113.189.58.141
[email protected] 123.89.158.214
[email protected] 113.192.203.187
[email protected] 112.18.58.145

输出样例 1:

[email protected]
111.192.203.187 1
112.192.203.187 1
113.192.203.187 1
114.192.203.187 1
115.192.203.187 1
[email protected]
218.109.231.189 2
112.18.58.145 1
113.189.58.141 1
123.89.158.214 1
[email protected]
218.109.231.189 2
112.18.58.145 1
113.189.58.141 1
123.89.158.214 1

输入样例 2:

24 5 8
[email protected] 218.109.231.189
[email protected] 112.192.203.187
[email protected] 112.18.235.143
[email protected] 112.192.203.187
[email protected] 113.18.235.143
[email protected] 111.192.203.187
[email protected] 218.109.231.189
[email protected] 111.18.235.143
[email protected] 115.192.203.187
[email protected] 113.189.58.141
[email protected] 111.192.203.187
[email protected] 112.18.58.145
[email protected] 114.192.203.187
[email protected] 112.18.235.143
[email protected] 123.89.158.214
[email protected] 112.18.235.143
[email protected] 218.109.231.189
[email protected] 113.192.203.187
[email protected] 218.109.231.189
[email protected] 114.192.203.187
[email protected] 113.189.58.141
[email protected] 123.89.158.214
[email protected] 113.192.203.187
[email protected] 112.18.58.145

输出样例 2:

[email protected]
111.192.203.187 1
112.192.203.187 1
113.192.203.187 1
114.192.203.187 1
115.192.203.187 1

解题思路:

大体上的思路就是先用嵌套的双层unordered_map把每个email账号各自的ip地址登录次数统计出来,然后再放入vector进行排序,具体步骤见代码注释。

代码(CPP):

#include 
using namespace std;
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e4 + 10;
const int INF = 0x3fffffff;
const int mod = 1000000007;
struct IP {
    string ip;
    int cnt;
    bool operator < (const IP &other) const {
        if (cnt == other.cnt) {
            return ip < other.ip;
        }
        return cnt > other.cnt;
    }
};
struct Info {
    string email;
    vector ips;
    int login;
    bool operator < (const Info &other) const {
        if (ips.size() == other.ips.size()) {
            return email < other.email;
        }
        return ips.size() > other.ips.size();
    }
};
vector inf;
vector ans;
unordered_map> mp;  // 统计
int n, tip, tlogin;

void solve() {
    cin >> n >> tip >> tlogin;
    // 输入并用map统计每个邮箱各自登录ip的登录次数
    for (int i = 1; i <= n; i++) {
        string email, ip;
        cin >> email >> ip;
        mp[email][ip]++;
    }
    // 枚举邮箱,将其放进vector容器,然后把各自的ip排序
    for (auto &e : mp) {
        vector ips;
        string email = e.first;
        auto ipmp = e.second;
        int cnt = 0;
        for (auto &v : ipmp) {
            ips.push_back({v.first, v.second});
            cnt += v.second;
        }
        sort(ips.begin(), ips.end());
        inf.push_back({email, ips, cnt});
    }
    // 挑选出合格的
    for (int i = 0; i < inf.size(); i++) {
        if (inf[i].ips.size() > tip && inf[i].login > tlogin) {
            ans.push_back(inf[i]);
        }
    }
    // ans
    if (ans.empty()) { // 如果没有合格的则输出最靠前的
        sort(inf.begin(), inf.end());
        for (int i = 0; i < inf.size() && inf[i].ips.size() == inf[0].ips.size(); i++) {
            cout << inf[i].email << endl;
            vector &ips = inf[i].ips;
            for (int j = 0; j < ips.size(); j++) {
                cout << ips[j].ip << " " << ips[j].cnt << endl;
            }
        }
    } else {
        sort(ans.begin(), ans.end());
        for (int i = 0; i < ans.size(); i++) {
            cout << ans[i].email << endl;
            vector &ips = ans[i].ips;
            for (int j = 0; j < ips.size(); j++) {
                cout << ips[j].ip << " " << ips[j].cnt << endl;
            }
        }
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout << fixed;
    cout.precision(18);

    solve();
    return 0;
}

你可能感兴趣的:(#,Robocom,CAIP,算法)