Trending Topic
Time limit: 1.000 seconds
Imagine you are in the hiring process for a company whose principal activity is the analysis of information in the Web. One of the tests consists in writing a program for maintaining up to date a set of trending topics. You will be hired depending on the efficiency of your solution. They provide you with text from the most active blogs. The text is organised daily and you have to provide the sorted list of the N most frequent words during the last 7 days, when asked.
INPUT
Each input file contains one test case. The text corresponding to a day is delimited by tag <text>. Queries of top N words can appear between texts corresponding to two different days. A top N query appears as a tag like <top 10 />. In order to facilitate you the process of reading from input, the number always will be delimited by white spaces, as in the sample.
Notes:
• All words are composed only of lowercase letters of size at most 20.
• The maximum number of different words that can appear is 20000.
• The maximum number of words per day is 20000.
• Words of length less than four characters are considered of no interest.
• The number of days will be at most 1000.
• 1 ≤ N ≤ 20
OUTPUT
The list of N most frequent words during the last 7 days must be shown given a query. Words must appear in decreasing order of frequency and in alphabetical order when equal frequency. There must be shown all words whose counter of appearances is equal to the word at position N. Even if the amount of words to be shown exceeds N.
SAMPLE INPUT
<text> imagine you are in the hiring process of a company whose main business is analyzing the information that appears in the web </text>
<text> a simple test consists in writing a program for maintaining up to date a set of trending topics </text>
<text> you will be hired depending on the efficiency of your solution </text>
<top 5 />
<text> they provide you with a file containing the text corresponding to a highly active blog </text>
<text> the text is organized daily and you have to provide the sorted list of the n most frequent words during last week when asked </text>
<text> each input file contains one test case the text corresponding to a day is delimited by tag text </text>
<text> the query of top n words can appear between texts corresponding to two different days </text>
<top 3 />
<text> blah blah blah blah blah blah blah blah blah please please please </text>
<top 3 />
SAMPLE OUTPUT
<top 5>
analyzing 1
appears 1
business 1
company 1
consists 1
date 1
depending 1
efficiency 1
hired 1
hiring 1
imagine 1
information 1
main 1
maintaining 1
process 1
program 1
simple 1
solution 1
test 1
that 1
topics 1
trending 1
whose 1
will 1
writing 1
your 1
</top>
<top 3>
text 4
corresponding 3
file 2
provide 2
test 2
words 2
</top>
<top 3>
blah 9
text 4
corresponding 3
please 3
</top>
解题:极其蛋疼狗血的破题目。
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <map> 13 #include <stack> 14 #define LL long long 15 #define pii pair<int,int> 16 #define INF 0x3f3f3f3f 17 using namespace std; 18 struct cao{ 19 string word; 20 int cnt; 21 cao(string x = "*",int y = 0){ 22 word = x; 23 cnt = y; 24 } 25 }; 26 map<string,int>mp; 27 string tmp; 28 vector<string>v[7]; 29 cao nima[200100]; 30 bool cmp(const cao &x,const cao &y){ 31 if(x.cnt == y.cnt) return x.word < y.word; 32 return x.cnt > y.cnt; 33 } 34 int main() { 35 mp.clear(); 36 int n = 0,i,j,ask,tot; 37 while(cin>>tmp){ 38 if(tmp == "<text>"){ 39 j = n%7; 40 for(i = 0; i < v[j].size(); i++) mp[v[j][i]]--; 41 v[j].clear(); 42 while(cin>>tmp){ 43 if(tmp == "</text>") break; 44 if(tmp.length() >= 4){ 45 mp[tmp]++; 46 v[j].push_back(tmp); 47 } 48 } 49 n++; 50 }else{ 51 cin>>ask>>tmp; 52 tot = 0; 53 for(map<string,int>::iterator it = mp.begin();it != mp.end(); it++) 54 nima[tot++] = cao(it->first,it->second); 55 sort(nima,nima+tot,cmp); 56 printf("<top %d>\n",ask); 57 for(i = 0,j = 0; i < ask; i++) 58 printf("%s %d\n",nima[i].word.c_str(),nima[i].cnt); 59 for(j = i,i--; j < tot && nima[j].cnt == nima[i].cnt; j++) 60 printf("%s %d\n",nima[j].word.c_str(),nima[j].cnt); 61 puts("</top>"); 62 } 63 } 64 return 0; 65 }