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

来源:互联网 发布:淘宝贷款逾期最坏结果 编辑:程序博客网 时间:2024/05/17 13:41


问题 A: 谍报分析

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

题目描述

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

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

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

输入

密文是由英语单词(小写字母)组成,有若干段。单词之间由一个或多个空格分开,自然段之后可以用一个“,”或“。”表示结束。整个内容的单词数量不超过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 8
soldiers 5
five 4
in 4
the 4
twenty 4
at 2
be 2
must 2
shanghai 2

#include <iostream>#include <stdio.h>#include <string>#include <map>#include <algorithm> using namespace std; struct frequency{    string word;    int num;}f[510];bool cmp(frequency f1,frequency f2){    if(f1.num == f2.num)        return f1.word<f2.word;    else        return f1.num>f2.num;}int main(){    int i;    string str;    map<string,int>m;    map<string,int>::iterator it;    while(cin>>str)    {        int len = str.length();        string s;        if(str[len-1]=='.'||str[len-1]==',')        {            s = str;            str = "";            for(i = 0; i < len-1; i++)                str += s[i];        }        m[str]++;      }    i = 0;    for(it = m.begin(); it != m.end(); it++)    {            f[i].word = it->first;            f[i].num = it->second;            i++;    }    sort(f,f+i,cmp);    for(int j = 0; j < i; j++)    {        if(j == 10)            break;        cout<<f[j].word<<" "<<f[j].num<<endl;    }    return 0;}


阅读全文
0 0
原创粉丝点击