涉及到STL里面map与set的使用,作为初学者的学习记录下来
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 ]
#include<map>
#include<set>
#include<iostream>
using namespace std;
int main() {
int n;
set<string> qqs, ips;
map<string, set<string> >qqip, ipqq;
string qq, ip;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> qq >> ip;
qqip[qq].insert(ip);
ipqq[ip].insert(qq);
}
cin >> ip >> qq;
if (ipqq.find(ip) == ipqq.end()) {
cout << "no qq" << endl;
} else {
cout << ip << " ==> ";
set<string>::iterator i = ipqq[ip].begin();
cout << "[ ";
while (i != ipqq[ip].end()) {
cout << *i << " ";
i++;
}
cout << "]" << endl;
}
if (qqip.find(qq) == qqip.end()) {
cout << "no ip" << endl;
} else {
cout << qq << " ==> ";
set<string>::iterator i = qqip[qq].begin();
cout << "[ ";
while (i != qqip[qq].end()) {
cout << *i << " ";
i++;
}
cout << "]" << endl;
}
return 0;
}