第十届河南省省赛- 问题 A: 谍报分析

来源:互联网 发布:剑灵天族枪手捏脸数据 编辑:程序博客网 时间:2024/05/17 11:32

问题 A: 谍报分析

时间限制: 1 Sec  内存限制: 128 MB
提交: 52  解决: 40
[提交][状态][讨论版]

题目描述

“八一三”淞沪抗战爆发后,***几次准备去上海前线视察和指挥作战。但都因为宁沪之间的铁路和公路遭到了敌军的严密封锁,狂轰滥炸,一直未能成行。

***特科组织,其主要任务是保卫***的安全,了解和掌握敌方的动向。经过一段时间的监听,谍报组获取了敌方若干份密报,经过分析,发现密文中频繁出现一些单词,情报人员试图从单词出现的次数中,推出敌军的行动计划。

请你编程,快速统计出频率高的前十个单词。

输入

密文是由英语单词(小写字母)组成,有若干段。单词之间由一个或多个空格分开,自然段之后可以用一个“,”或“。”表示结束。整个内容的单词数量不超过10000,不同的单词个数不超过500.

输出

输出占10行,每行一个单词及出现的次数,中间一个空格。要求按频率降序输出,出现次数相同的单词,按字典序输出。

样例输入

shooting is at shanghai station. shooting  must  be carried out. shooting  shooting.shanghai station must be surrounded,  at least a team of  one hundred  soldiers to fight.  twenty  five soldiers shooting in the north, twenty  five soldiers shooting in the south,  twenty  five soldiers  shooting in  the east, twenty  five soldiers shooting in the west.

样例输出

shooting 8soldiers 5five 4in 4the 4twenty 4at 2be 2must 2shanghai 2

提示


#include<iostream>#include<string.h>#include<cstdio>#include<map>#include<algorithm>using namespace std;struct node{    string b;    int count;}w[500];bool cmp(node x, node y){    if(x.count==y.count)        return x.b<y.b;    else        return x.count>y.count;}int main(){    char a[1000];    int i;    map<string,int>q;    map<string,int>::iterator it;    while(scanf("%s",a)!=EOF)    {        int n;        n=strlen(a);        if(a[n-1]==','||a[n-1]=='.')            a[n-1]='\0';        q[a]++;    }    i=0;    for(it=q.begin();it!=q.end();it++)    {        string m;        m=it->first;        w[i].b= m.c_str();        w[i].count=it->second;        i++;    }    int t;    t=i;    sort(w,w+t,cmp);    for(i=0;i<9;i++)    {        cout<<w[i].b<<" "<<w[i].count<<endl;    }    cout<<w[9].b<<" "<<w[9].count;    return 0;}


原创粉丝点击