zoukankan      html  css  js  c++  java
  • 新浪微博热门话题(30 分)(字符串)

    新浪微博热门话题(30 分)

    新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。

    本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。

    输入格式:

    输入说明:输入首先给出一个正整数N(105​​),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,如果长度超过40个字符,则只保留前40个字符。输入保证#成对出现。

    输出格式:

    第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出And k more ...,其中k是另外几条热门话题的条数。输入保证至少存在一条话题。

    注意:两条话题被认为是相同的,如果在去掉所有非英文字母和数字的符号、并忽略大小写区别后,它们是相同的字符串;同时它们有完全相同的分词。输出时除首字母大写外,只保留小写英文字母和数字,并用一个空格分隔原文中的单词。

    输入样例:

    4
    This is a #test of topic#.
    Another #Test of topic.#
    This is a #Hot# #Hot# topic
    Another #hot!# #Hot# topic
    

    输出样例:

    Hot
    2
    And 1 more ...

    这题对字符串处理要求比较多,在字符串比较的时候要遵守一定规则(字母和数字相同即相同),但是在输出时却要原样输出,而且同一行中一个话题不可以加入两次,这样没办法使用cstring里面的函数
    比较尴尬,通过这题学了一个分离字符串的函数strtok,这个函数和python里面得split函数差不多,都是把一个字符串分隔成以规定字符间隔得多个字符串,下面附上第一次做的源码:这次没有考虑
    到比较规则。。这题我搜了一下网上也没有答案,对于题意我还有一点疑问,就是当两个按规则比较相等的字符串为出现次数最多的热门话题时,也输出字典序小的那个吗??


      1 #include<cstdio>
      2 #include<cstdlib>
      3 #include<cstring>
      4 #include<iostream>
      5 #include<string>
      6 using namespace std;
      7 #define MAXN 10005
      8 typedef long long LL;
      9 /*
     10 
     11 */
     12 
     13 typedef struct node
     14 {
     15     char id[41];
     16     int cnt;
     17     int line;
     18     struct node* next;
     19 }*List;
     20 typedef struct tb
     21 {
     22     int Tablesize;
     23     List *list;
     24 }*Hashlist;
     25 LL Hash(char key[],LL size)
     26 {
     27     LL tmp = 0;
     28     for(LL i=13;i<18;i++)
     29     {
     30         if( !( (key[i]<='z'&&key[i]>='a')||(key[i]<='9'&&key[i]>='0') ))
     31             continue;
     32         if(key[i]=='x')
     33             tmp = (tmp*10+10)%size;
     34         else
     35             tmp = (tmp*10 + key[i]-'0')%size;
     36     }
     37     if(tmp>=0)
     38         return tmp;
     39     else
     40         return (tmp+size)%size;
     41 }
     42 int NextPrime(int x)
     43 {
     44     int i; 
     45     for (int Next = x; ; Next++)
     46     {
     47         for (i = 2; i * i <= Next; i++)
     48             if (Next % i == 0)
     49                 break;
     50         if (i * i > Next)
     51             return Next;
     52     }
     53 }
     54 Hashlist Init(int size)
     55 {
     56     Hashlist H = (Hashlist)malloc(sizeof(tb));
     57     H->Tablesize = NextPrime(size);
     58     H->list = (List*)malloc(sizeof(List)*H->Tablesize);
     59     for(int i=0;i< H->Tablesize;i++)
     60     {
     61         H->list[i] =(List)malloc(sizeof(node));
     62         H->list[i]->next = NULL;
     63         H->list[i]->cnt = 0;
     64         H->list[i]->line = -1;
     65     }
     66     return H;
     67 }
     68 List Find(char key[],Hashlist H)
     69 {
     70     List t = H->list[Hash(key,H->Tablesize)];
     71     List p = t->next;
     72     while(p!=NULL && strcmp(key,p->id))
     73         p = p->next;
     74     return p;
     75 }
     76 void Insert(char key[],Hashlist H,int line)
     77 {
     78     int len = strlen(key);
     79     for(int i=0;i<len;i++)
     80         key[i] = tolower(key[i]);
     81     //cout<<key<<endl;
     82     List t = H->list[Hash(key,H->Tablesize)];
     83     List f = Find(key,H);
     84     if(f==NULL)
     85     {
     86         List tmp = (List)malloc(sizeof(node));
     87         tmp->cnt = 1;
     88         tmp->line = line;
     89         strcpy(tmp->id,key);
     90         tmp->next = t->next;
     91         t->next = tmp;
     92     }
     93     else
     94     {
     95         if((f->line)!=line)
     96             (f->cnt)++;
     97     }
     98 }
     99 void Findmax(Hashlist H)
    100 {
    101     int max = -1,same = 1;
    102     char ans[41];
    103     for(int i=0;i< H->Tablesize;i++)
    104     {
    105         List t = H->list[i];
    106         List p = t->next;
    107         while(p!=NULL)
    108         {
    109             if(p->cnt>max)
    110             {
    111                 max = p->cnt;
    112                 same = 1;
    113                 strcpy(ans,p->id);
    114             }
    115             else if(p->cnt==max)
    116             {
    117                 if(strcmp(ans,p->id)>0)
    118                     strcpy(ans,p->id);
    119                 same++;
    120             }
    121             p = p->next;
    122         }
    123     }
    124     if(ans[0]<='z'&&ans[0]>='a')
    125         ans[0] = toupper(ans[0]);
    126     printf("%s
    %d
    ",ans,max);
    127     if(same>1)
    128         printf("And %d more ...
    ",same-1);
    129 }
    130 int main()
    131 {
    132     int n;
    133     char str[141];
    134     scanf("%d",&n);
    135     Hashlist H = Init(n);
    136     getchar();
    137     for(int l=1;l<=n;l++)
    138     {
    139         gets(str);
    140         char * p;
    141         p = strtok(str,"#");
    142         int cnt = 1;
    143         while(p!=NULL)
    144         {
    145             if(cnt%2==0)
    146                 Insert(p,H,l);
    147             cnt++;
    148             p = strtok(NULL,"#");
    149         }
    150     }
    151     Findmax(H);
    152     return 0;
    153 }
     1 #include <cstdio>  
     2 #include <cstring>  
     3 #include <iostream>  
     4 #include <algorithm>  
     5 #include <string>  
     6 #include <map>  
     7 using namespace std;  
     8 map<string, int> vis;  
     9 map<string, int> cnt;  
    10 map<string, int>::iterator it;  
    11 bool judge(char c) {  
    12     if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || '0' <= c && c <= '9' || c == ' ') return true;  
    13     return false;  
    14 }  
    15 int main() {  
    16     int n; scanf("%d", &n); getchar();  
    17     char str[1000];  
    18     string s;  
    19     for (int i = 1; i <= n; i++) {  
    20         gets(str);  
    21         bool flag = false;  
    22         vis.clear();  
    23         for (int i = 0; str[i]; i++) {  
    24             if (str[i] == '#' && flag == false) {  
    25                 s.clear(); flag = true;  
    26             }  
    27             else if (str[i] != '#' && flag == true) {  
    28                 if (!judge(str[i])) str[i] = ' ';  
    29                 if ('A' <= str[i] && str[i] <= 'Z') str[i] += 32;  
    30                 int len = s.size();  
    31                 if (len > 0 && str[i] == ' ' && s[len - 1] == ' ') continue;  
    32                 s.push_back(str[i]);  
    33             }  
    34             else if (str[i] == '#' && flag == true) {  
    35                 string ss;  
    36                 if (s[0] == ' ') for (int i = 1; i < s.size(); i++) ss.push_back(s[i]);  
    37                 else if (s[s.size() - 1] == ' ') for (int i = 0; i < s.size() - 1; i++) ss.push_back(s[i]);  
    38                 else ss = s;  
    39                 //cout << ss << endl;  
    40                 if (!vis[ss]) {  
    41                     vis[ss] = 1;  
    42                     cnt[ss]++;  
    43                 }  
    44                 flag = false;  
    45             }  
    46         }  
    47     }  
    48     int num = 0;  
    49     int res = 0;  
    50     string ans;  
    51     for (it = cnt.begin(); it != cnt.end(); it++) {  
    52         string x = it->first; int y = it->second;  
    53         if (y > res) {  
    54             num = 0;  
    55             res = y;  
    56             ans = x;  
    57         }  
    58         else if (y == res) num++;  
    59     }  
    60     ans[0] -= 32;  
    61     cout << ans << endl << res << endl;  
    62     if (num) cout << "And " << num << " more ..." << endl;  
    63 } 
    View Code
     
  • 相关阅读:
    mysql5.7 linux安装参考
    谈谈微服务中的 API 网关(API Gateway)
    十大Intellij IDEA快捷键
    SqoopFlume、Flume、HDFS之间比较
    PostgreSQL-存储过程(一)基础篇
    spark调优篇-oom 优化(汇总)
    spark调优篇-数据倾斜(汇总)
    spark调优篇-Spark ON Yarn 内存管理(汇总)
    spark异常篇-OutOfMemory:GC overhead limit exceeded
    spark异常篇-Removing executor 5 with no recent heartbeats: 120504 ms exceeds timeout 120000 ms 可能的解决方案
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8552511.html
Copyright © 2011-2022 走看看