Skip to content

Instantly share code, notes, and snippets.

@BeMg
Last active November 2, 2017 03:22
Show Gist options
  • Select an option

  • Save BeMg/d30cb6682a5c43bf1f5bebee21861ce2 to your computer and use it in GitHub Desktop.

Select an option

Save BeMg/d30cb6682a5c43bf1f5bebee21861ce2 to your computer and use it in GitHub Desktop.
getCount
#include <bits/stdc++.h>
using namespace std;
const int MAX_HASH = 100000000;
bool cmp(pair<string, int> a, pair<string, int> b) {
return a.second > b.second;
}
int main() {
unordered_map<string, int> mp;
char tmp[100000];
while(fgets(tmp, 100000, stdin)!=NULL) {
char* ptr = NULL;
ptr = strtok(tmp, "\n\t ");
while(ptr!=NULL) {
mp[string(ptr)] += 1;
ptr = strtok(NULL, "\n\t ");
}
}
vector<pair<string, int>> v;
for(auto i: mp) {
v.push_back(pair<string, int>(i.first, i.second));
}
sort(v.begin(), v.end(), cmp);
for(auto i: v) {
printf("%d\t%s\n", i.second, i.first.c_str());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment