2021 Robocom 决赛 第三题

文章提供了一个关于程序设计实验中的自动安全预警功能实现,包括输入输出样例以及解题思路,主要涉及数据结构和账户登录行为分析。
摘要由CSDN通过智能技术生成

原题链接:

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
daohaole@qq.com 218.109.231.189
1jiadelaolao@163.com 112.192.203.187
chenyuelaolao@zju.edu.cn 112.18.235.143
jiadelaolao@163.com 112.192.203.187
chenyuelaolao@zju.edu.cn 113.18.235.143
jiadelaolao@163.com 111.192.203.187
daohaole@qq.com 218.109.231.189
chenyuelaolao@zju.edu.cn 111.18.235.143
1jiadelaolao@163.com 115.192.203.187
daohaole@qq.com 113.189.58.141
1jiadelaolao@163.com 111.192.203.187
daohaole@qq.com 112.18.58.145
1jiadelaolao@163.com 114.192.203.187
chenyuelaolao@zju.edu.cn 112.18.235.143
daohaole@qq.com 123.89.158.214
chenyuelaolao@zju.edu.cn 112.18.235.143
youdaohaole@qq.com 218.109.231.189
jiadelaolao@163.com 113.192.203.187
youdaohaole@qq.com 218.109.231.189
jiadelaolao@163.com 114.192.203.187
youdaohaole@qq.com 113.189.58.141
youdaohaole@qq.com 123.89.158.214
1jiadelaolao@163.com 113.192.203.187
youdaohaole@qq.com 112.18.58.145

输出样例 1:

1jiadelaolao@163.com
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
daohaole@qq.com
218.109.231.189 2
112.18.58.145 1
113.189.58.141 1
123.89.158.214 1
youdaohaole@qq.com
218.109.231.189 2
112.18.58.145 1
113.189.58.141 1
123.89.158.214 1

输入样例 2:

24 5 8
daohaole@qq.com 218.109.231.189
1jiadelaolao@163.com 112.192.203.187
chenyuelaolao@zju.edu.cn 112.18.235.143
jiadelaolao@163.com 112.192.203.187
chenyuelaolao@zju.edu.cn 113.18.235.143
jiadelaolao@163.com 111.192.203.187
daohaole@qq.com 218.109.231.189
chenyuelaolao@zju.edu.cn 111.18.235.143
1jiadelaolao@163.com 115.192.203.187
daohaole@qq.com 113.189.58.141
1jiadelaolao@163.com 111.192.203.187
daohaole@qq.com 112.18.58.145
1jiadelaolao@163.com 114.192.203.187
chenyuelaolao@zju.edu.cn 112.18.235.143
daohaole@qq.com 123.89.158.214
chenyuelaolao@zju.edu.cn 112.18.235.143
youdaohaole@qq.com 218.109.231.189
jiadelaolao@163.com 113.192.203.187
youdaohaole@qq.com 218.109.231.189
jiadelaolao@163.com 114.192.203.187
youdaohaole@qq.com 113.189.58.141
youdaohaole@qq.com 123.89.158.214
1jiadelaolao@163.com 113.192.203.187
youdaohaole@qq.com 112.18.58.145

输出样例 2:

1jiadelaolao@163.com
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 <bits/stdc++.h>
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<IP> 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<Info> inf;
vector<Info> ans;
unordered_map<string, unordered_map<string, int>> 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<IP> 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<IP> &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<IP> &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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值