Zipf's Law(map容器)

Zipf's Law
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 1767 Accepted: 532

Description

Harvard linguistics professor George Kingsley Zipf (1902-1950) observed that the frequency of the kth most common word in a text is roughly proportional to 1/k. He justified his observations in a book titled Human behavior and the principle of least effort published in 1949. While Zipf's rationale has largely been discredited, the principle still holds, and others have afforded it a more sound mathematical basis.
You are to find all the words occurring n times in an English text. A word is a sequence of letters. Words are separated by non-letters. Capitalization should be ignored. A word can be of any length that an English word can be.

Input

Input consists of several test cases. The first line of each case contains a single positive integer n. Several lines of text follow which will contain no more than 10000 words. The text for each case is terminated by a single line containing EndOfText. EndOfText does not appear elsewhere in the input and is not considered a word.

Output

For each test case, output the words which occur n times in the input text, one word per line, lower case, in alphabetical order. If there are no such words in input, output the following line:
There is no such word.

Leave a blank line between cases.

Sample Input

2

In practice, the difference between theory and practice is always
greater than the difference between theory and practice in theory.
	- Anonymous

Man will occasionally stumble over the truth, but most of the
time he will pick himself up and continue on.
        - W. S. L. Churchill
EndOfText

Sample Output

between
difference
in
will

Source

map容器,很轻松啊!
 
#include<cstdio>
#include<iostream>
#include<string>
#include<sstream>
#include<cstring>
#include<map>
using namespace std;
int main()
{
    string s,buf;
    int i,f,n,num=0;
    while(cin>>n)
    {
        f=0;
        map<string,int>mp;
        while (cin>>s&&s!="EndOfText")
        {
             for(i=0;i<s.length();i++)
             {
                  if (isalpha(s[i]))
                  {
                      s[i]=tolower(s[i]);
                  }
                  else
                  {
                      s[i]=' ';
                  }
             }
              stringstream ss(s);
              while (ss>>buf)
              {
                  mp[buf]++;
              }
        }
        if(num!=0)
        {
            cout<<endl;
        }
        for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++)
        {
            if(it->second==n)
            {
                cout<<it->first<<endl;
                f=1;
            }
        }
        if(f==0)
        {
            cout<<"There is no such word."<<endl;
        }
        num++;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值