-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob_00860.cpp
82 lines (76 loc) · 1.78 KB
/
prob_00860.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <map>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <stdlib.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <climits>
#include <list>
#include <forward_list>
#define rep(i,a,b) for(i=a;i<b;++i)
#define vectIter(vect) for(auto &val: vect)
#define mapIter(map_) for(auto &val_: map_)
#define gc getchar_unlocked
using namespace std;
#define SIZE 100000
#define INF 999999999
struct functor{
bool operator()(const char* str1, const char* str2){
return strcmp(str1,str2)<0;
}
};
map<const char*, int, functor> word_map;
int lambda;
void extract_words(char* str){
char* delimiters=".,:;!?\"() \t\n";
// convert string to lowercase
for(int i=0;str[i]!='\0';++i) str[i]= tolower(str[i]);
char* token= strtok(str,delimiters);
while(token!=NULL){
char* temp= strdup(token);
if(word_map.find(temp)==word_map.end())
word_map[temp]=1;
else ++word_map[token];
token= strtok(NULL,delimiters);
++lambda;
}
}
double get_entropy(){
double entropy=0;
double log_lambda= log10(lambda);
for(auto& val: word_map)
entropy+=(val.second*(log_lambda- log10(val.second)));
entropy/= lambda;
double rel_entropy= (entropy/log_lambda)*100;
printf("%.1lf %.0lf\n",entropy,rel_entropy);
return entropy;
}
int main(){
char *eot= "****END_OF_TEXT****";
char *eoi= "****END_OF_INPUT****";
int i,j,k,len;
char str[SIZE];
while(true){
fgets(str,SIZE-1,stdin);
if(str[strlen(str)-1]=='\n')
str[strlen(str)-1]='\0';
if(strcmp(str,eoi)==0)
break;
lambda=0;
word_map.erase(word_map.begin(),word_map.end());
while(strcmp(str,eot)!=0){
extract_words(str);
fgets(str,SIZE-1,stdin);
str[strlen(str)-1]='\0';
}
cout << lambda <<" ";
get_entropy();
}
}