題目:
新浪微博可以在發言中嵌入“話題”,即將發言中的話題文字寫在一對“#”之間,就可以生成話題鏈接,點擊鏈接可以看到有多少人在跟自己討論相同或者相似的話題。新浪微博還會隨時更新熱門話題列表,並將最熱門的話題放在醒目的位置推薦大家關注。
本題目要求實現一個簡化的熱門話題推薦功能,從大量英文(因為中文分詞處理比較麻煩)微博中解析出話題,找出被最多條微博提到的話題。
輸入格式:
輸入說明:輸入首先給出一個正整數N(≤105),隨后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 ...
思路:
首先處理兩個‘#’之間的字符串,是字母,數字的保留,字母、數字之間的其他符號統統用一個空格代替。另外開頭和結尾的空格去掉。之后判斷就可以了。
感謝該博客提供了卡我一中午的樣例:https://blog.csdn.net/henuni/article/details/75907043
樣例輸入:
4
This is a #test of 1 topic#.
Another #Test of (1)topic.#
This is a #Hot# topic
This is a test of 1 topic
樣例輸出:
Test of 1 topic
2
卡掉一中午的代碼:(因為沒有考慮最后一個字符為‘#’的情況)
#include <bits/stdc++.h>
using namespace std; typedef long long ll; const ll MOD = 2147493647; const int maxn = 1e5+10; typedef pair<string,int> pir; vector<pir> v; map<string,int> mp; string makeString(string str){ string temp = "",res = ""; for(int i = 0; i<str.length(); i++){ if(i==0 && str[i]==' ')continue; if(i==str.length()-1 && str[i]==' ')continue; temp+=str[i]; } return temp; } void init() { int n; string str; v.push_back(pir("@",1000)); cin>>n; getchar(); for(int kk = 0; kk<n; kk++) { getline(cin,str); string tmp = ""; set<string> s; set<string>::iterator it; for(int i = 0; i<str.length(); i++) { if(str[i]=='#') { i++; while(str[i]!='#' && i<str.length()) { if(isupper(str[i])) { tmp += tolower(str[i]); i++; } else if(islower(str[i])) { tmp += str[i]; i++; }else if(str[i]>='0'&&str[i]<='9'){ tmp += str[i]; i++; } else if(str[i]==' ') {//aaa#aa@@@@bb#
while(i+1<str.length() && str[i+1]==' ')i++; tmp += str[i];//str[i];
i++; } else i++; } if(i>=str.length()) break; else if(str[i]=='#') { tmp = makeString(tmp); //cout<<tmp<<endl;
if(tmp!="") s.insert(tmp); tmp = ""; } } } for(it=s.begin(); it!=s.end(); it++) { if(mp[*it]==0) { v.push_back(pir(*it,1)); mp[*it] = v.size()-1; } else { int in = mp[*it]; v[in].second++; } } s.clear(); } } bool cmd(pir a, pir b){ if(a.second == b.second) return a.first<b.first; return a.second > b.second; } int main() { init(); sort(v.begin()+1,v.end(),cmd); int index = 0; for(int i = 2; i<v.size(); i++){ if(v[i].second == v[1].second) index++; } string ans = v[1].first; ans[0] = ans[0]-'a'+'A'; cout<<ans<<endl; cout<<v[1].second<<endl; if(index>0) cout<<"And "<<index<<" more ..."<<endl; return 0; }
完全通過的代碼:
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 2147493647; const int maxn = 1e5+10; typedef pair<string,int> pir; vector<pir> v; map<string,int> mp; string makeString(string str){ string temp = "",res = ""; for(int i = 0; i<str.length(); i++){ if(i==0 && str[i]==' ')continue; if(i==str.length()-1 && str[i]==' ')continue; temp+=str[i]; } return temp; } bool judge(char ch){ if(isupper(ch) || islower(ch) || (ch>='0'&&ch<='9') || ch=='#') return false; return true; } void init() { int n; string str; v.push_back(pir("@",1000)); cin>>n; getchar(); for(int kk = 0; kk<n; kk++) { getline(cin,str); string tmp = ""; set<string> s; set<string>::iterator it; for(int i = 0; i<str.length(); i++) { if(str[i]=='#') { i++; while(str[i]!='#' && i<str.length()) { if(isupper(str[i])) { tmp += tolower(str[i]); i++; } else if(islower(str[i])) { tmp += str[i]; i++; }else if(str[i]>='0'&&str[i]<='9'){ tmp += str[i]; i++; } else { while(i+1<str.length() && judge(str[i+1]))i++; tmp += ' ';//str[i]; i++; } } if(i>=str.length()) break; else if(str[i]=='#') { tmp = makeString(tmp); //cout<<tmp<<endl; s.insert(tmp); tmp = ""; } } } for(it=s.begin(); it!=s.end(); it++) { if(mp[*it]==0) { v.push_back(pir(*it,1)); mp[*it] = v.size()-1; } else { int in = mp[*it]; v[in].second++; } } s.clear(); } } bool cmd(pir a, pir b){ if(a.second == b.second) return a.first<b.first; return a.second > b.second; } int main() { init(); sort(v.begin()+1,v.end(),cmd); int index = 0; for(int i = 2; i<v.size(); i++){ if(v[i].second == v[1].second) index++; } string ans = v[1].first; ans[0] = ans[0]-'a'+'A'; cout<<ans<<endl; cout<<v[1].second<<endl; if(index>0) cout<<"And "<<index<<" more ..."<<endl; return 0; }

