第十届ACM省赛-A 谍报分析

问题 A: 谍报分析

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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

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

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

输入

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

思路:

题目主要是以文件格式的输入, 所以读入时 ctrl+z 结束即可   
利用map的迭代器 和 结构体排序  操作即可


#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
    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]++;    //用map存放
    }
    i=0;
    for (it=q.begin();it!=q.end();it++)
    {
        string m;
        m = it->first;
        w[i].b = m.c_str();   //map中的string 是const 类型 不能赋值  先变换成char型字符串 .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;
}





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值