SPOILER/CHEAT: C++ faster solution

Nicholas Leippe nick at byu.edu
Fri Mar 10 16:37:25 MST 2006


// this is nearly the same as before, but some additional thought and
// reorganization yields substantially increased performance

#include <fstream>
#include <iostream>
#include <map>
#include <set>

using namespace std;

string<char> dictionary("/usr/share/dict/words");

int main(int argc, char** argv) {
	map<string, int> words;
	map<string, int> count;
	ifstream w(argv[1]);
	ifstream d(dictionary.c_str());
	string word;

	while (w >> word) {
		words[word]++;
	}

	map<string, int>::iterator i;
	while (d >> word) {
		i = words.find(word);
		if (i != words.end()) {
			count[word] = (*i).second;
		}
	}

	for (i = count.begin(); i != count.end(); i++)
	{
		cout << (*i).first << ' ' << (*i).second << endl;
	}
	return 0;
};

-- 
Respectfully,

Nicholas Leippe
Sales Team Automation, LLC
1335 West 1650 North, Suite C
Springville, UT  84663 +1 801.853.4090
http://www.salesteamautomation.com



More information about the PLUG mailing list