-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.cpp
56 lines (49 loc) · 1.23 KB
/
test.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
#include <bits/stdc++.h>
#include <string>
using namespace std;
// you just need to implement this function
string mostCommonWord(string document) {
// your code here
string s;
transform(document.begin(), document.end(), document.begin(), ::tolower);
unordered_map<string,pair<int,vector<int>>>mpp;
int i;
for(i=0;i<document.size();i++){
if(document[i]==' '){
mpp[s].first++;
mpp[s].second.push_back(i);
s="";
}else{
s+=document[i];
}
}
mpp[s].first++;
mpp[s].second.push_back(i);
int max=0;
int min=INT_MAX;
for(auto x:mpp){
if(x.second.first>max){
max=x.second.first;
}
}
string ans;
// for(auto x:mpp){
// cout<<x.first<<" "<<x.second.first<<" "<<x.second.second[0]<<endl;
// }
for(auto x:mpp){
if(x.second.first==max){
if(x.second.second[0]<min){
min=x.second.second[0];
ans=x.first;
}
}
}
return ans;
}
int main() {
string document;
getline(cin, document);
// please do not change the below code
cout << mostCommonWord(document);
return 0;
}