蓝桥杯不支持unordered_map
当然可以写一个结构体来自定义规则结合map来实现unordered_map
不过需要reverse正序(下面给出该结构体写法)
不然只好乖乖用vector
看了前面的大佬的题解(我过了后看)
istringstream不香吗?
虽说这个类比较慢,但是照样过

  1. //定义无序map时
  2. //map<string, int, DisableCompare<string> >
  3. //来实现类似unorder_map<string, int>
  4. template <class T>
  5. struct DisableCompare : public binary_function<T, T, bool>
  6. {
  7. bool operator()(T L, T R)const
  8. {
  9. static bool disablecompare = false;
  10. if (L == R)
  11. {
  12. return false;
  13. }
  14. if (disablecompare)
  15. {
  16. disablecompare = false;
  17. return false;
  18. }
  19. else
  20. {
  21. disablecompare = true;
  22. return true;
  23. }
  24. }
  25. };

想必你看到这样结构体可能会头昏
下面给出我AC代码

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4. #include <vector>
  5. #include <sstream> //提供istringstream类
  6. #include <algorithm>
  7. using namespace std;
  8. struct node {
  9. string str;
  10. int cnt;
  11. node(string str, int cnt = 1) : str(str), cnt(cnt) {}
  12. bool operator==(string s) const {//重置==实现查找结构体某一个元素(48行)
  13. return s == this->str;
  14. }
  15. };
  16. typedef vector<node>::iterator vit;
  17. int main() {
  18. vector<node> a;
  19. string s;
  20. int len = -1;
  21. getline(cin, s);
  22. if (s.empty()) {
  23. return 0;
  24. }
  25. else {
  26. istringstream is(s); //利用istringstream快速分解字符串
  27. while (!is.eof()) {
  28. string t;
  29. is >> t;
  30. if (t.empty()) {
  31. break;
  32. }
  33. if (!isalpha(t[t.size() - 1])) {
  34. //t.pop_back(); //C++11标准,蓝桥杯不支持
  35. t.erase(t.size() - 1); //只好使用erase
  36. }
  37. len = max((int)(t.size()), len);
  38. for (int i = 0; i < t.size(); i++) {
  39. if (islower(t[i])) {
  40. t[i] -= 32;
  41. }
  42. }
  43. vit it = find(a.begin(), a.end(), t);
  44. if (it != a.end()) {
  45. it->cnt++;
  46. }
  47. else {
  48. a.push_back(node(t));
  49. }
  50. }
  51. for (int i = 0; i < a.size(); i++) {
  52. for (int j = 0; j < len - a[i].str.size(); j++) {
  53. cout << " ";
  54. }
  55. cout << a[i].str << ':';
  56. for (int j = 0; j < a[i].cnt; j++) {
  57. cout << '*';
  58. }
  59. cout << a[i].cnt << endl;
  60. }
  61. return 0;
  62. }
  63. }

这里多说两句
如果使用istringstream以空格为基准分字符串直接使用>>即可(参考第34行)
如果是其他的则是getline(is, t, "分割字符")(变量名以上面的代码为准)
刚来这个网站
代码如果有什么不足之处,请指正

点赞(0)
 

9.9 分

3 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论