上网统计
在一个网络系统中有 N 个用户、 M 次上网记录。每个用户可以自己注册一个用户名,每个用户名只包含小写字母且长度小于 1000 的字符串,每次上网日志里都会有记录,现在请统计每个用户每次浏览了多少个网页。注意,有可能有用户没有访问记录。
输入:
第 1 行包含两个正整数 N (1 ≤ N ≤ 1000) 和 M (1 ≤ M ≤ 5000)。
第 2 ~ M+1 行,每行包含 2 个字符串,分别表示用户名和浏览的网页名。
输出:
共 x 行,x 表示有访问记录的用户数量,
每行的第一个字符串时用户名,接下来的若干字符串是这个用户依次浏览的网页名(之间用一个空格隔开)。
按照用户名出现的次序排序输出。
样例:
1 2 3 4 5 6 7 8
| 5 7 goodstudyer bookshopa likespacea spaceweb goodstudyer bookshopb likespaceb spaceweb likespacec spaceweb likespacea juiceshop gameplayer gameweb
|
1 2 3 4 5
| goodstudyer bookshopa bookshopb likespacea spaceweb juiceshop likespaceb spaceweb likespacec spaceweb gameplayer gameweb
|
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| #include<iostream> #include<string> #include<vector> #include<unordered_map> using namespace std;
int cnt; int n; int recordNum; unordered_map<string, int> mp;
int main() { cin >> n >> recordNum; vector<vector<string>> vec(n, vector<string>{}); for (int i = 0; i < recordNum; ++i) { string username, siteinfo; cin >> username >> siteinfo; if (mp.count(username) == 0) { mp[username] = cnt; vec[cnt].push_back(username); vec[cnt].push_back(siteinfo); cnt++; } else { vec[mp[username]].push_back(siteinfo); } } for (int i = 0; i < n; ++i) { for (int j = 0; j < vec[i].size(); ++j) { if (j) cout << " "; cout << vec[i][j]; } cout << endl; } return 0; }
|