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())) != '\n')
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] = '\0';
41                 else s[sp] = '\0';
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 }

 

posted @ 2020-05-02 17:25  幻想Elapse  阅读(312)  评论(1编辑  收藏  举报