map暴力。

。。

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 />

2

Problem IProblem I

Trending Topic

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>

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <map>
#include <vector> using namespace std; typedef pair<int,int> pII; map<string,int> Hash;
vector<int> dy[11];
string rHash[20200];
int day_sum[11][20200];
char cache[30];
int now=9,pre=0,id=1;
int arr[20020],na;
string rss[20020];
bool vis[20020]; void DEBUG(int x)
{
int sz=dy[x].size();
for(int i=0;i<sz;i++)
{
cout<<"ID: "<<dy[x][i]<<" : "<<rHash[dy[x][i]]<<endl;
cout<<"sum: "<<day_sum[x][dy[x][i]]<<endl;
}
} struct RSP
{
int times;
string word;
}rsp[20020]; bool cmpRSP(RSP a,RSP b)
{
if(a.times!=b.times)
return a.times>b.times;
else
return a.word<b.word;
} void get_top(int now,int k)
{
int sz=dy[now].size();
na=0;
int _7dayago=(now+3)%10;
memset(vis,false,sizeof(vis));
for(int i=0;i<sz;i++)
{
if(vis[dy[now][i]]==false)
{
arr[na++]=day_sum[now][dy[now][i]]-day_sum[_7dayago][dy[now][i]];
vis[dy[now][i]]=true;
}
}
sort(arr,arr+na);
int sig=arr[max(0,na-k)];
int rn=0;
memset(vis,false,sizeof(vis));
for(int i=0;i<sz;i++)
{
int times=day_sum[now][dy[now][i]]-day_sum[_7dayago][dy[now][i]];
if(times >= sig &&vis[dy[now][i]]==false)
{
rsp[rn++]=(RSP){times,rHash[dy[now][i]]};
vis[dy[now][i]]=true;
}
}
sort(rsp,rsp+rn,cmpRSP);
printf("<top %d>\n",k);
for(int i=0;i<rn;i++)
{
cout<<rsp[i].word<<" "<<rsp[i].times<<endl;
}
printf("</top>\n");
} int main()
{
while(scanf("%s",cache)!=EOF)
{
if(strcmp(cache,"<text>")==0)
{
///read cache
pre=now;
now=(now+1)%10;
dy[now]=dy[pre];
memcpy(day_sum[now],day_sum[pre],sizeof(day_sum[0]));
///7 day ago ....
while(scanf("%s",cache))
{
if(cache[0]=='<') break;
if(strlen(cache)<4) continue;
string word=cache;
if(Hash[word]==0)
{
rHash[id]=word;
Hash[word]=id++;
}
int ID=Hash[word];
if(day_sum[pre][ID]==0)
dy[now].push_back(ID);
day_sum[now][ID]++;
}
}
else if(strcmp(cache,"<top")==0)
{
int top;
scanf("%d",&top); scanf("%s",cache);
get_top(now,top);
}
}
return 0;
}

最新文章

  1. 浅谈js的事件冒泡机制
  2. java虚拟机和Dalvik虚拟机的区别
  3. JAVA中的聚集和组合的区别和联系
  4. linux C(hello world) 解方程
  5. js 判断对象相等
  6. Android系统简介(中):系统架构
  7. html5新特性--音频视频,拖放
  8. Js中单引号和双引号的区别
  9. css基础之 font的简写规则 以及 自定义 CSS3 @font-face详细用法
  10. android handler机制和Timer采用
  11. Linux平台下使用rman进行oracle数据库迁移
  12. [置顶] JAVA从零单排4-----继承、封装和多态详解
  13. AJAX的原理
  14. react 阻止事件冒泡
  15. html iframe高度自适应
  16. postman headers 请求参数和MD5加密签名
  17. 【转载】关于.NET下开源及商业图像处理(PSD)组件
  18. Redis学习系列二之.Net开发环境搭建及基础数据结构String字符串
  19. ReactNative踩坑日志——页面跳转之——Undefined is not an Object(evaluating this2.props.navigation.navigate)
  20. C# 获取文件夹下的所有文件的文件名

热门文章

  1. IIS 7.0、IIS 7.5 和 IIS 8.0 使用的 HTTP 状态代码【转载自微软官方】
  2. nodejs 中使用 mysql 实现 crud
  3. R 连接mysql数据库
  4. Deutsch lernen (07)
  5. swift里 as、as!、as?区别 T.Type与动态类型
  6. java HttpURLConnection 登录网站 完整代码
  7. python tips:最内嵌套作用域规则,闭包与装饰器
  8. Xilinx 7系列FPGA部分重配置【2】
  9. appium分层自动化的封装
  10. Codeforces Round #548 (Div. 2) A. Even Substrings