• 7-46 新浪微博热门话题 (30分)--map


     1 #include<iostream>
     2 #include <map>
     3 #include <string>
     4 #include <cstring>
     5 using namespace std;
     6 map<string, long long int> num;//某话题出现次数
     7 int main()
     8 {
     9     long long int N;
    10     cin >> N;
    11     cin.get();//吸收回车
    12     char ch;
    13     char s[150];//临时储存字符串
    14     int sp = 0;//sp的指针
    15     long long int sum = 0;//还有sum条并列热门话题
    16     long long int max = 0;//最热门话题出现次数
    17     char hot[150];//存储最热门的话题
    18     for (long long int i = 0; i < N; i++)
    19     {
    20         map<string, bool>flag;//标记字符串是否在本行出现过
    21         while ((ch = tolower(cin.get())) != '
    ')
    22         {
    23             if (ch == '#')
    24             {
    25                 sp = 0;
    26                 while ((ch = tolower(cin.get())) != '#')
    27                 {
    28                     if ((ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'))
    29                     {
    30                         s[sp++] = ch;
    31                     }
    32                     else
    33                     {
    34                         if (sp != 0 && (s[sp - 1] >= 'a' && s[sp - 1] <= 'z') || (s[sp - 1] >= '0' && s[sp - 1] <= '9'))
    35                         {
    36                             s[sp++] = ' ';
    37                         }
    38                     }
    39                 }
    40                 if (sp != 0 && s[sp - 1] == ' ')s[sp - 1] = '';
    41                 else s[sp] = '';
    42                 if (!flag[s])
    43                 {
    44                     num[s]++;
    45                         flag[s] = true;
    46                     if (num[s] > max)
    47                     {
    48                         max = num[s];
    49                         sum = 0;
    50                         strcpy(hot, s);
    51                     }
    52                     else if (num[s] == max)
    53                     {
    54                         sum++;
    55                         if (strcmp(s, hot) < 0)
    56                             strcpy(hot, s);
    57                     }
    58                 }
    59             }
    60         }
    61     }
    62     hot[0] = toupper(hot[0]);
    63     cout << hot << endl << max << endl;
    64     if (sum > 0)
    65     {
    66         cout << "And " << sum << " more ...";
    67     }
    68     return 0;
    69 }
  • 相关阅读:
    2021/9/20 开始排序算法
    快速排序(自己版本)
    2021/9/17(栈实现+中后缀表达式求值)
    2021/9/18+19(中缀转后缀 + 递归 迷宫 + 八皇后)
    20212021/9/13 稀疏数组
    2021/9/12 线性表之ArrayList
    开发环境重整
    Nginx入门
    《财富的帝国》读书笔记
    Linux入门
  • 原文地址:https://www.cnblogs.com/2020R/p/12818762.html
Copyright © 2020-2023  润新知