XYNUOJ 第四次比赛 情报分析

问题 D: 情报分析

时间限制: 1 Sec   内存限制: 12 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 
#include <algorithm>    
#include <iostream>    
#include <string>    
#include <map>    
#include<cstring>   
#include <cstdio>    
using namespace std;    
struct node//定义结构体存放各个单词的情况    
{    
    char str[100];    
    int count;    
}word[1000];    
int N=0;    
int cmp(node w1,node w2)    
{    
    if(w1.count!=w2.count)//比较出现数目的多少,大者在前,小者在后    
        return w1.count>w2.count;    
    else//如果出现数目相等,按照字典序排序    
    {    
        int a=strcmp(w1.str,w2.str);    
        if(a>0)    
            return 0;    
        else    
            return 1;    
    }    
}    
int main()    
{    
    map <string,int> m;//定义map来接收并记录输入的数据    
    map <string,int>::iterator it;//定义迭代器    
    char str[100];    
    while(scanf("%s",str)!=EOF)    
    {    
        int len=strlen(str);    
        if(str[len-1]==','||str[len-1]=='.')//如果末尾字符是','或'.',将其清除    
        	str[len-1]='\0';    
        string s=str;    
        m[s]++;//将此单词出现的次数++    
    }    
    for(it=m.begin(); it!=m.end(); it++)    
    {    
        string s=(*it).first;    
        int count=(*it).second;    
        strcpy(word[N].str,s.c_str());    
        word[N++].count=count;//将map中存放的数据放入结构体中    
    }    
    sort(word,word+N,cmp);//将结构体排序    
    for(int i=0; i<10; i++)//输出前10个元素    
    {    
        printf("%s %d\n",word[i].str,word[i].count);    
    }    
    return 0;    
}  
#include<iostream>
#include<bits/stdc++.h>//万能头文件,包含许多头文件,但是会影响程序 
using namespace std;
typedef pair<string, int> PAIR;
struct CmpByValue
{
	bool operator()(const PAIR& leftValue, const PAIR& rightValue)
	{
		return leftValue.second > rightValue.second? 1:(leftValue.second != rightValue.second)?0:leftValue.first<=rightValue.first;
	}
};
map<string ,int> mapWord;
int main()
{
	string str;
	while(cin>>str)
	{
		if(str[str.length()-1] == ',' || str[str.length()-1] == '.')
			str = str.substr(0,str.length()-1);
		mapWord[str]++;
	}
	vector<PAIR> vec(mapWord.begin(), mapWord.end());
	sort(vec.begin(), vec.end() ,CmpByValue());
	int i = 0;
	for(auto it=vec.begin(); it!=vec.end(); ++it,++i)
	{
		if(i > 9)break;
		cout<<it->first<<" "<<it->second<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值