文章

43

粉丝

180

获赞

32

访问

200.6k

头像
c++
P1452 北京理工大学机试题
发布于2022年4月30日 15:45
阅读数 5.3k

  1. #include <iostream>
  2. #include <map>
  3. #include <algorithm>
  4. using namespace std;
  5. const int N = 1010;
  6. char x;
  7. string s;
  8. map<string, int> h;
  9. struct node
  10. {
  11. string s;
  12. int cnt;
  13. }r[N];
  14. bool cmp(node a, node b)
  15. {
  16. return a.cnt > b.cnt;
  17. }
  18. int main()
  19. {
  20. int n = 1010;
  21. while (n -- )
  22. {
  23. scanf("%c", &x);
  24. if (x != ' ' && x != ',' && x != '.')
  25. {
  26. if (x >= 'A' && x <= 'Z') s += x - 'A' + 'a';
  27. else s += x;
  28. }
  29. else
  30. {
  31. h[s] ++;
  32. s.clear();
  33. }
  34. }
  35. int p = 0;
  36. for (auto& a : h) if (a.first != "") r[p].s = a.first, r[p ++ ].cnt = a.second;
  37. sort(r, r + p, cmp);
  38. for (int i = 0; i < p; i ++ ) cout << r[i].s << ":" << r[i].cnt << endl;
  39. return 0;
  40. }

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发