Created
September 11, 2010 05:41
-
-
Save vo/574879 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem C: Choose Your Words Carefully | |
// Author: Christopher Vo | |
#include <iostream> | |
#include <cstdio> | |
#include <algorithm> | |
#include <map> | |
using namespace std; | |
typedef map<string, int> hist; | |
// compare histogram entries by their frequency | |
bool lessThan(const pair<string, int> & l, const pair<string, int> & r) { | |
return l.second < r.second; | |
} | |
int main() | |
{ | |
hist hist; | |
string line; | |
// process input -------------------------------------------- | |
while (getline(cin, line)) { | |
char c; | |
int bw = 0, curr = 0, end = line.length(); | |
if (end == 0) continue; | |
// skip to first alphanumeric in line | |
while (curr <= end && !isalnum(line[curr])) curr++; | |
bw = curr; | |
// go through rest of line | |
do { | |
c = line[curr]; | |
// non-alphanumeric and EOL marks end of words | |
if (!isalnum(c) || curr == end) { | |
string word = line.substr(bw, curr - bw); | |
for (int i = 0; i < word.length(); i++) | |
word[i] = tolower(word[i]); | |
// update histogram | |
hist[word]++; | |
// advance bw and curr to beginning of next word | |
while (curr <= end && !isalnum(line[curr])) curr++; | |
bw = curr; | |
} | |
curr++; | |
} while (curr <= end); | |
} | |
// determine most-used word --------------------------------- | |
int max = max_element(hist.begin(), hist.end(), lessThan)->second; | |
cout << max << " occurrences" << endl; | |
// print out all matching words, in any order. -------------- | |
hist::iterator it; | |
for(it = hist.begin(); it != hist.end(); ++it) | |
if(it->second >= max) | |
cout << it->first << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Problem C: Choose Your Words Carefully | |
// Author: Christopher Vo | |
import java.util.*; | |
public class C { | |
public static void main(String args[]) { | |
HashMap<String, Integer> map = new HashMap<String, Integer>(); | |
Scanner sc = new Scanner(System.in).useDelimiter("[^a-zA-Z0-9]+"); | |
while (sc.hasNext()) { | |
String word = sc.next().toLowerCase(); | |
Integer c = map.get(word); | |
map.put(word, c != null ? c + 1 : 1); | |
} | |
int max = Collections.max(map.values()); | |
System.out.println(max + " occurrences"); | |
for(Map.Entry<String, Integer> e : map.entrySet()) { | |
if(e.getValue().equals(max)) | |
System.out.println(e.getKey()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment