24h購物| | PChome| 登入
2013-06-18 08:52:59| 人氣2,741| 回應0 | 上一篇 | 下一篇

[UVA] 10126 - Zipf's Law

推薦 0 收藏 0 轉貼0 訂閱站台

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.

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

Output for Sample Input

between
difference
in
will


#include <stdio.h>
#include <map>
#include <iostream>
#include <sstream>
#include <string.h>
using namespace std;
int main() {
int n, cases = 0;
int i, m;
string line;
while(scanf("%d", &n) == 1) {
if(cases) puts("");
cases++;
map<string, int> R;
while(getline(cin, line)) {
if(line == "EndOfText") break;
m = line.length();
for(i = 0; i < m; i++) {
if(line[i] >= 'a' && line[i] <= 'z'){}
else if(line[i] >= 'A' && line[i] <= 'Z')
line[i] = line[i]+32;
else
line[i] = ' ';
}
stringstream sin(line);
while(sin >> line)
R[line]++;
}
int found = 0;
for(map<string, int>::iterator it = R.begin();
it != R.end(); it++) {
if(it->second == n) {
cout << it->first << endl;
found = 1;
}
}
if(!found)
puts("There is no such word.");
}
return 0;
}
/*
2
a-a
EndOfText
*/
 

台長: Morris
人氣(2,741) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][循環] 11701 - Cantor
此分類上一篇:[UVA] 11452 - Dancing the Cheeky-Cheeky

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文