7-3 账户安全预警(unordered_map 哈希表嵌套哈希表)

我的天,第一次用visual studio,还行,最优秀的地方就是小的语法错误指示得地方超清楚,对于使用迭代器访问first等等非常友好!都是DEV不支持unordered_map,怎么不支持呢!!!
好叭,DEV中要使用unordered_map,需要以下两条语句

#include <tr1/unordered_map>
using namespcace std:tr1;

但是DEV真的不支持for(auto x: mp)访问容器,但是vs可以
在这里插入图片描述
输入样例 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
#include<iostream>
#include<stdio.h>
#include<string>
#include<algorithm>


using namespace std;
#include <unordered_map>

//unordered_map <string,unordered_map<string,int> > mp;
unordered_map<string, unordered_map<string, int> > mp;
struct node {
	string ip;
	int cnt;
	node(string ip, int cnt) :ip(ip), cnt(cnt) {}
	bool operator<(const node& n) {
		if (n.cnt == cnt)return ip < n.ip;//string可以直接比较
		return  n.cnt <cnt;
	}
};//哈希表无序,输出需按有序 ip+次数 
struct record {
	string email;
	vector<node> v;
	int num;
	record(string email, vector<node> v, int num) :email(email), v(v), num(num) {}
	bool operator<(const record& n) {
		if (n.num == num)return email < n.email;
		return  n.num <num;
	}
};
signed main() {
	mp.reserve(7000);
	//	ios::sync_with_stdio(false);
	//	cin.tie(0);居然不能输入字符 
	int n, t1, t2;
	cin >> n >> t1 >> t2;
	//n条登录数,每个邮箱对应的ip不超过t1,邮箱登录的次数 
	string str1, str2;
	for (int i = 0; i < n; i++) {
		cin >> str1 >> str2;
		//		if(mp.count(?) )mp.insert(str1,{str2,1})
		mp[str1][str2]++;//nice,哈希表的映射,
//		不用判重删除插入新的,直接更改

	}
	//	邮箱对应ip,相同的ip对应着次数
	//	邮箱,ip;这样存可以记录登录次数,但不能记录不同的ip数及其对应的次数
	//	邮箱,ip,cnt,要是三个在一块记录就ok
	//	要统计相同的合并在一起,显然用容器比较好,
		Email1 bacd ,Email1 bacd  Email1{(bacd,2),(abcd,1)}
	//	容器嵌套容器,哈希表嵌套哈希表
	//	for(auto &m : mp) 

	vector<record> rec;
	vector<record> warn;
	unordered_map<string, unordered_map<string, int> >::iterator it = mp.begin();
	for (; it != mp.end(); it++) {
		string str = it->first;
		int cnt = 0;
		//		if(mp.count(str)>=t1&&mp[str])
		vector<node> v;//不管是否是预警都要记录下来,万一没一个预警的,也要输出ip最多的账户的登录信息 
		unordered_map<string, int>::iterator jt = it->second.begin();
		for (; jt != it->second.end(); jt++) {
			v.push_back({ jt->first, jt->second });
			cnt += jt->second;
		}
		sort(v.begin(), v.end());
		rec.push_back({ str,v,cnt });

	}

	for (int i = 0; i < rec.size(); i++) {
		if (rec[i].v.size() <= t1 || rec[i].num <= t2)continue;
		else warn.push_back(rec[i]);
	}
	if (warn.size()) {
		//输出所有预警的用户 ,按ip数,email字典序 
	//	对于每个预警用户,ip登录次数,ip字典序 
		sort(warn.begin(), warn.end());
		for (int i = 0; i < warn.size(); i++) {
			vector<node> v = warn[i].v;
			cout << warn[i].email << endl;
			for (int i = 0; i < v.size(); i++)cout << v[i].ip << " " << v[i].cnt << endl;
		}
	}
	else {//没有预警用户 
		sort(rec.begin(), rec.end());
		int t = rec[0].v.size();

		for (int i = 0; i < rec.size(); i++) {
			if (rec[i].v.size() == t) {//ip数最大的可能有多个(一样多 
				cout << rec[i].email<<endl;
				for (int j = 0; j < t; j++) {
					cout << rec[i].v[i].ip << " " << rec[i].v[i].cnt << endl;
				}
			}
		}
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值