PTA 7-18 新浪微博热门话题

网友投稿 565 2022-11-28

PTA 7-18 新浪微博热门话题

PTA 7-18 新浪微博热门话题

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

MAP瞎暴

#include #include #include #include #include #include using namespace std;map vis;map cnt;map::iterator it;bool judge(char c) { if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || '0' <= c && c <= '9' || c == ' ') return true; return false;}int main() { int n; scanf("%d", &n); getchar(); char str[1000]; string s; for (int i = 1; i <= n; i++) { gets(str); bool flag = false; vis.clear(); for (int i = 0; str[i]; i++) { if (str[i] == '#' && flag == false) { s.clear(); flag = true; } else if (str[i] != '#' && flag == true) { if (!judge(str[i])) str[i] = ' '; if ('A' <= str[i] && str[i] <= 'Z') str[i] += 32; int len = s.size(); if (len > 0 && str[i] == ' ' && s[len - 1] == ' ') continue; s.push_back(str[i]); } else if (str[i] == '#' && flag == true) { string ss; if (s[0] == ' ') for (int i = 1; i < s.size(); i++) ss.push_back(s[i]); else if (s[s.size() - 1] == ' ') for (int i = 0; i < s.size() - 1; i++) ss.push_back(s[i]); else ss = s; //cout << ss << endl; if (!vis[ss]) { vis[ss] = 1; cnt[ss]++; } flag = false; } } } int num = 0; int res = 0; string ans; for (it = cnt.begin(); it != cnt.end(); it++) { string x = it->first; int y = it->second; if (y > res) { num = 0; res = y; ans = x; } else if (y == res) num++; } ans[0] -= 32; cout << ans << endl << res << endl; if (num) cout << "And " << num << " more ..." << endl;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:POJ 3140 Contestants Division——树形dp
下一篇:PTA 7-22 模拟EXCEL排序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~