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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章