單詞統計

單詞統計

1000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
Font Size:

現在有一段文本需要處理,需要你編寫一個小程序,能夠讀取文本中的所有內容,把其中出現的每個單詞的統計出來,最後輸出滿足要求的單詞。一個單詞由一段連續的英文字母組成,單詞與單詞之間由一個或多個空格、tab或回車分隔。單詞在統計個數時不區分大小寫。

Input

輸入是一段英文文章。英文文章的單詞數不超過2000.每個單詞的長度不超過25。

Output

輸出所有出現次數大於或等於3次的單詞,每個單詞一行。單詞以全小寫方式輸出。 單詞按字典序,從小到大輸出。(字典序表示兩個單詞字符串調用strcmp比較所得到的大小順序)。 如果實際滿足要求的單詞沒有,則輸出一個無內容的文件。 如果實際滿足要求的單詞數超過10個,則輸出字典序前10個單詞。

Sample Input

Beijing Normal University has a history of more than one hundred years  
which is almost as long as the history of Chinese modern education  
Beijing Normal University has developed from the Faculty of Education 
Capital Metropolitan University established under the 
concept of Establish school prioritize teacher education 
which initiated teacher training in Chinese higher education
After several times of merging and reforming especially Beijing 
Normal University has moved into the new age of rapid development
Beijing Normal University history is an epitome of the development of 
modern teacher training and Chinese higher education

Sample Output

beijing
chinese
education
has
history
normal
of
teacher
the
university

容器無敵啊!
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
int min(int a,int b)
{
    return a<b?a:b;
}
int main()
{
    int i;
    map<string,int>mp;
    vector<string>vec;
    char str[33];
    mp.clear();
    while(scanf("%s",str)!=EOF)
    {
        int n = strlen(str);
        for(i=0;i<n;i++)
        {
            str[i]=tolower(str[i]);
        }
        mp[str]++;
        if(mp[str]==3)
        {
            vec.push_back(str);
        }
    }
    sort(vec.begin(),vec.end());
    for(i=0;i<min(vec.size(),10);i++)
    {
        cout<<vec[i].c_str()<<endl;
    }
    return 0;
}

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章