首页 > 学院 > 开发设计 > 正文

ZCMU-Problem D - Zipf's Law

2019-11-11 04:59:33
字体:
来源:转载
供稿:网友

PRoblem D: Problem D - Zipf's Law

Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 5[Submit][Status][Web Board]

Description

Problem D - Zipf's Law

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.

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

2In practice, the difference between theory and practice is alwaysgreater than the difference between theory and practice in theory.- AnonymousMan will occasionally stumble over the truth, but most of thetime he will pick himself up and continue on.- W. S. L. ChurchillEndOfText

Sample Output

betweendifferenceinwill【解析】这道题就是输入一个n和一段文本,在输入到EndOfText表示此段文本输入结束,然后判断输入的这段文本当中有哪些单词出现了两次。所以我们其实可以用map<string,int>来做,进行统计就好了,而且这个就是按照字典序来排序下去的。所以我们统计完直接遍历就可以了,这里需要拥有stringsteam来进行拆解除每一个单词。
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<map>#include<sstream>#include<algorithm>using namespace std;map<string,int>a;int main(){    int n,flag=0,i,flag1;    string s;    while(~scanf("%d",&n))    {        a.clear();        if(flag==0)        {            flag=1;//在第2次以及以后的输入n之后都要先输出一个空行        }        else if(flag==1)        {            printf("/n");        }        while(getline(cin,s))//逐行读取        {            if(s=="EndOfText")                break;            else            {                for(i=0;i<s.size();i++)                {                    if(isalpha(s[i]))                        s[i]=tolower(s[i]);                    else                        s[i]=' ';/*这里一定要注意不是字母的就一定要变成空格                        不然其他字符出现了n次也会输出*/                }            }            stringstream s1(s);            string s2;            while(s1>>s2)//以空格为边界            {                a[s2]++;            }        }        flag1=0;//标记有没有出现过n次的单词        for(map<string,int>::iterator it=a.begin();it!=a.end();it++)        {            if(it->second==n)            {                flag1=1;                cout<<it->first<<endl;            }        }        if(flag1==0)            printf("There is no such word./n");//表示没有这样的单词    }    return 0;}
上一篇:PAT BASIC 1004

下一篇:共享文件问题

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表