首页 > 代码库 > HLG 2113 Count (C++ --- map容器 基础题)
HLG 2113 Count (C++ --- map容器 基础题)
链接: http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2113
Description
Given a number of strings, can you find how many strings that appears T times?
Input
The input contains multiple test cases. Each case begins with a integer N(the number of strings you will get, N<=100000), followed by N lines, each consists of a string.
The length of each string won‘t longer than 20.
Output
For each test case:
There will be several lines. Echo line print two integers T and M, separated by a space.
T represents the string appears T times, M represents there are M strings that echo string appears T times.
Don‘t print T or M if M <= 0. The output is ordered by T, from small to large.
Sample Input
5
BBA
BBA
BEA
DEC
CCF
Sample Output
1 3
2 1
比赛时写的代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <map> #include <algorithm> #include <string> using namespace std; map <string, int> mp; map <string, int>:: iterator it; int flag[100000]; int main() { int n; string s; while(~scanf("%d", &n)) { mp.clear(); for(int i=0; i<n; i++) { cin >> s; mp[s]++; } memset(flag, 0, sizeof(flag)); //sort(mp.begin(), mp.end(); cmp); for(it=mp.begin(); it!=mp.end(); it++) { int mc = (*it).second; flag[mc]++; } for(int i=0; i<100000; i++) if(flag[i]) { cout << i << " " << flag[i] << endl; } } return 0; }