Descrption
An ip can login several qqs, and a qq can be logined by several ips.
Your task is to find which qqs have been logined by the ip and which ips have
logined the qq.
output format : qq ==> [ ip1 ip2 … ] and ip ==> [ qq1 qq2 … ]
if no such qq or ip, output “no qq” and “no ip”
First input n, then n+2 lines follow…
n lines:
qq ip
2 lines:
ip // find which qqs have been logined by the ip
qq // find which ips have logined the qq.
sample input:
5
10258279649 192.168.1.45
10258279649 192.168.1.45
10258279643 192.168.1.40
10258279640 192.168.1.45
10258279641 192.168.1.30
192.168.1.45
10258279649
sample output:
192.168.1.45 ==> [ 10258279640 10258279649 ]
10258279649 ==> [ 192.168.1.45 ]
题目大意是一个IP可以登陆好几个QQ,一个QQ可以在好几个IP地址登陆。现在要找出某个IP登陆了多少QQ,和一个QQ在哪些IP地址登陆了。
这题就是用map,没什么难的。直接贴代码了。
#include<iostream>
#include <vector>
#include <string>
#include <map>
#include<set>
#include <algorithm>
using namespace std;
int main()
{
int n;
string _ip;
string qq;
set<string> IP;
set<string> QQ;
map<string, vector<string>> _mapQQ;
map<string, vector<string>> _mapIP;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> qq >> _ip;
_mapQQ[qq].push_back(_ip);
_mapIP[_ip].push_back(qq);
}
cin >> _ip >> qq;
if (!_mapIP[_ip].empty())
{
for (int i = 0; i < _mapIP[_ip].size(); i++)
QQ.insert(_mapIP[_ip][i]);
}
if (!_mapQQ[qq].empty())
{
for (int i = 0; i < _mapQQ[qq].size(); i++)
IP.insert(_mapQQ[qq][i]);
}
string sip_qq;
string sqq_ip;
sip_qq += _ip;
sip_qq += " ==> [ ";
set<string>::iterator it; //定义前向迭代器
for (it = QQ.begin(); it != QQ.end(); it++)
{
sip_qq += *it;
sip_qq += " ";
}
sip_qq += "]";
sqq_ip += qq;
sqq_ip += " ==> [ ";
for (it = IP.begin(); it != IP.end(); it++)
{
sqq_ip += *it;
sqq_ip += " ";
}
sqq_ip += "]";
if (QQ.empty())
cout << "no qq"<<endl;
else {
cout << sip_qq << endl;
}
if (IP.empty())
cout << "no ip" << endl;
else
{
cout << sqq_ip << endl;
}
return 0;
}