USACO 1.2.3 – Name That Number



#include <iostream>
#include <fstream>
#include <string>

using namespace std;

long long int encode(string s) {
	long long int code = 0;
	for (size_t i = 0; i < s.length(); ++i) {
		if (s[i] == 'Q' || s[i] == 'Z')
			return 1;
			code = code*10 +  (s[i] < 'Q' ? (((s[i] - 'A') / 3) + 2) : (((s[i] - 'Q') / 3) + 7));
	}
	return code;
}

int main_namenum() {
	ofstream fout("namenum.out");
	ifstream fin("namenum.in"), dict("dict.txt");

	long long int code, found = 0;
	fin >> code;
	string entry;
	while (dict >> entry)
		if ( encode(entry) == code && (found=1))
			fout << entry << endl;

	if (!found)
		fout << "NONE" << endl;

	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: