PTA新浪微博热门话题c++版——山东科技大学

本文介绍了一个简化版热门话题推荐系统的实现方法,该系统可以从大量英文微博中解析并统计话题的提及次数,最终输出提及次数最多的话题及其提及次数。

题目:
新浪微博可以在发言中嵌入“话题”,即将发言中的话题文字写在一对“#”之间,就可以生成话题链接,点击链接可以看到有多少人在跟自己讨论相同或者相似的话题。新浪微博还会随时更新热门话题列表,并将最热门的话题放在醒目的位置推荐大家关注。
本题目要求实现一个简化的热门话题推荐功能,从大量英文(因为中文分词处理比较麻烦)微博中解析出话题,找出被最多条微博提到的话题。
输入格式:
输入说明:输入首先给出一个正整数N(≤1e​5​​),随后N行,每行给出一条英文微博,其长度不超过140个字符。任何包含在一对最近的#中的内容均被认为是一个话题,输入保证#成对出现。
输出格式:
第一行输出被最多条微博提到的话题,第二行输出其被提到的微博条数。如果这样的话题不唯一,则输出按字母序最小的话题,并在第三行输出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 

一开始没注意到这个题有很多坑点,看了别人的代码后才明白
参考这个大佬的代码
STL真好,迭代器真好
别忘了getchar

#include<bits/stdc++.h>
using namespace std;
map<string,int> mp;
string s;
string set_string(string ss)
{
    string cnt;
    bool is_repeat=false;
    for(int i=0; i<ss.length(); i++)
    {
        if(ss[i]>='A'&&ss[i]<='Z')
        {
            cnt+=(ss[i]-'A'+'a');
            is_repeat=false;
        }
        else if((ss[i]>='a'&&ss[i]<='z')||(ss[i]>='0'&&ss[i]<='9'))
        {
            cnt+=ss[i];
            is_repeat=false;
        }
        else if(!is_repeat)
        {
            cnt+=' ';
            is_repeat=true;
        }
    }
    if(cnt[cnt.length()-1]==' ')
        cnt.erase(cnt.end()-1);
    return cnt;
}
string cmp(string s1,string s2)
{
    return s1<s2?s1:s2;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n;
    cin>>n;
    getchar();//又特么的忘了
    while(n--)
    {
        map<string,int> small_map;
        string ss;
        getline(cin,s);
        for(int i=0; i<s.length(); i++)
        {
            ss="";
            if(s[i]=='#')
            {
                i++;
                while(s[i]!='#')
                    ss+=s[i++];
                ss=set_string(ss);
            }
            if(ss=="")
                continue;
            small_map[ss]++;
        }
        map<string,int>::iterator it;
        for(it=small_map.begin(); it!=small_map.end(); it++)
        {
            mp[it->first]++;
        }
    }
    int maxn=0,repeat=0;
    map<string,int>::iterator it;
    for(it=mp.begin();it!=mp.end();it++)
    {
        if(it->second>maxn)
        {
            maxn=it->second;
            s=it->first;
            repeat=0;
        }
        else if(it->second==maxn)
        {
            s=cmp(s,it->first);
            repeat++;
        }
    }
    cout<<(char)(s[0]-'a'+'A');
    for(int i=1;i<s.length();i++)
        cout<<s[i];
    cout<<endl<<maxn<<endl;
    if(repeat)
        cout<<"And "<<repeat<<" more ..."<<endl;
}

更多PTA代码请到我的博客里参考

ps:代码仅供参考,请勿抄袭

要使用C语言解决PTA 7 - 15新浪微博热门话题题目,需要完成以下几个主要步骤:读取输入、提取话题、统计话题出现次数、找出热门话题。以下是一个示例代码框架: ```c #include <stdio.h> #include <string.h> #include <ctype.h> #define MAX_TOPICS 1000 #define MAX_LENGTH 200 // 定义话题结构体 typedef struct { char topic[MAX_LENGTH]; int count; } Topic; // 查找话题是否已经存在 int findTopic(Topic topics[], int topicCount, char *newTopic) { for (int i = 0; i < topicCount; i++) { if (strcmp(topics[i].topic, newTopic) == 0) { return i; } } return -1; } // 提取话题 void extractTopics(char *line, Topic topics[], int *topicCount) { int len = strlen(line); int inTopic = 0; char currentTopic[MAX_LENGTH]; int topicIndex = 0; for (int i = 0; i < len; i++) { if (line[i] == '#') { if (inTopic) { currentTopic[topicIndex] = '\0'; // 转换为大写 for (int j = 0; currentTopic[j]; j++) { currentTopic[j] = toupper(currentTopic[j]); } int found = findTopic(topics, *topicCount, currentTopic); if (found == -1) { strcpy(topics[*topicCount].topic, currentTopic); topics[*topicCount].count = 1; (*topicCount)++; } else { topics[found].count++; } inTopic = 0; topicIndex = 0; } else { inTopic = 1; } } else if (inTopic) { currentTopic[topicIndex++] = line[i]; } } } // 比较函数,用于qsort int compare(const void *a, const void *b) { Topic *topicA = (Topic *)a; Topic *topicB = (Topic *)b; if (topicA->count != topicB->count) { return topicB->count - topicA->count; } return strcmp(topicA->topic, topicB->topic); } int main() { int n; scanf("%d", &n); getchar(); // 消耗掉换行符 Topic topics[MAX_TOPICS]; int topicCount = 0; for (int i = 0; i < n; i++) { char line[MAX_LENGTH]; fgets(line, MAX_LENGTH, stdin); extractTopics(line, topics, &topicCount); } // 排序话题 qsort(topics, topicCount, sizeof(Topic), compare); // 输出结果 printf("%s\n", topics[0].topic); printf("%d\n", topics[0].count); int sameCount = 0; for (int i = 1; i < topicCount; i++) { if (topics[i].count == topics[0].count) { sameCount++; } } if (sameCount > 0) { printf("And %d more ...\n", sameCount); } return 0; } ``` ### 代码说明: 1. **结构体定义**:定义了`Topic`结构体,用于存储话题和其出现的次数。 2. **提取话题**:`extractTopics`函数用于从输入的字符串中提取话题,并将其存储到`topics`数组中。 3. **查找话题**:`findTopic`函数用于查找话题是否已经存在于`topics`数组中。 4. **排序**:使用`qsort`函数对话题按出现次数进行排序,如果出现次数相同,则按字典序排序。 5. **输出结果**:输出出现次数最多的话题及其出现次数,如果有多个话题出现次数相同,则输出额外信息。 ### 复杂度分析: - **时间复杂度**:$O(n * m * log(m))$,其中$n$是输入的行数,$m$是不同话题的数量。 - **空间复杂度**:$O(m)$,主要用于存储不同的话题。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值