USACO Training>My Training>Section 1.2>Name That Number
Name That Number
Among the large Wisconsin cattle ranchers, it is customary to brand
cows with serial numbers to please the Accounting Department. The cow
hands don't appreciate the advantage of this filing system, though, and
wish to call the members of their herd by a pleasing name rather than
saying, "C'mon, #4734, get along."
Help the poor cowhands out by writing a program that will translate
the brand serial number of a cow into possible names uniquely associated
with that serial number. Since the cow hands all have cellular saddle
phones these days, use the standard Touch-Tone(R) telephone keypad
mapping to get from numbers to letters (except for "Q" and
"Z"):
2: A,B,C 5: J,K,L 8: T,U,V
3: D,E,F 6: M,N,O 9: W,X,Y
4: G,H,I 7: P,R,S
Acceptable names for cattle are provided to you in a file named
"dict.txt", which contains a list of fewer than 5,000
acceptable cattle names (all letters capitalized). Take a cow's
brand number and report which of all the possible words to which
that number maps are in the given
dictionary which is supplied as dict.txt in the grading
environment (and is sorted into ascending order).
For instance, the brand number 4734 produces all the following names:
GPDG GPDH GPDI GPEG GPEH GPEI GPFG GPFH GPFI GRDG GRDH GRDI
GREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEI
GSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFI
HRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDI
HSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEI
IPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFI
ISDG ISDH ISDI ISEG ISEH ISEI ISFG ISFH ISFI
As it happens, the only one of these 81 names that is in the list
of valid names is "GREG".
Write a program that is given the brand number of a cow and
prints all the valid names that can be generated from that brand
number or ``NONE'' if there are no valid names. Serial numbers can
be as many as a dozen digits long.
PROGRAM NAME: namenum
INPUT FORMAT
A single line with a number from 1 through 12 digits in length.
SAMPLE INPUT (file namenum.in)
4734
OUTPUT FORMAT
A list of valid names that can be generated from the input, one per
line, in ascending alphabetical order.
SAMPLE OUTPUT (file namenum.out)
GREG
Solution (Me)
/*
ID: sgospod1
PROG: namenum
LANG: C++
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <string>
#define STANDARD_INPUT
#define MAX_ACCEPTABLE_NAMES 5000
#define MAX_DIGITS 12
using namespace std;
char buffer[MAX_DIGITS+1];
long long translated;
long long N;
char maxname[MAX_DIGITS+1];
long long CharToKey(char ch)
{
switch(ch)
{
case 'A':
case 'B':
case 'C':
return 2;
case 'D':
case 'E':
case 'F':
return 3;
case 'G':
case 'H':
case 'I':
return 4;
case 'J':
case 'K':
case 'L':
return 5;
case 'M':
case 'N':
case 'O':
return 6;
case 'P':
case 'R':
case 'S':
return 7;
case 'T':
case 'U':
case 'V':
return 8;
case 'W':
case 'X':
case 'Y':
return 9;
}
/* what to do if char is invalid */
return 0;
}
char KeyToMaxChar(int key)
{
switch(key)
{
case 2:
return 'C';
case 3:
return 'F';
case 4:
return 'I';
case 5:
return 'L';
case 6:
return 'O';
case 7:
return 'S';
case 8:
return 'V';
case 9:
return 'Y';
}
/* what to do if key is invalid */
return '\0';
}
long long Translate(char *name)
{
int i = 0;
long long r = CharToKey(name[0]);
for(i=1; i<MAX_DIGITS; i++)
{
char c = name[i];
if(c=='\0') break;
r = r*10 + CharToKey(c);
}
return r;
}
void ToMaxName(long long num, char *mxn)
{
int idx = 0;
char temp[MAX_DIGITS+1];
while(num>0)
{
int rem = num%10;
num = num/10;
temp[idx++] = KeyToMaxChar(rem);
}
for(int i=0, j=idx-1; i<idx; i++, j--) mxn[i] = temp[j];
mxn[idx] = '\0';
}
int CompareStrings(char *str1, char *str2)
{
for(int idx=0; idx<MAX_DIGITS; idx++)
{
char c1 = str1[idx];
char c2 = str2[idx];
if(c1>c2) return 1;
else if(c1<c2) return -1;
else if(c1=='\0' && c2=='\0') return 0;
}
return 0;
}
/*
ISANNE
ISHA
ISFI
*/
int main()
{
#ifdef STANDARD_INPUT
istream &fin = cin;
ostream &fout = cout;
ifstream din ("dict.txt");
#else
ofstream fout ("namenum.out");
ifstream fin ("namenum.in");
ifstream din ("dict.txt");
#endif
fin >> N;
ToMaxName(N, maxname);
cerr << "maxname: " << maxname << endl;
bool bFound = false;
while(!din.eof())
{
memset(buffer, 0, sizeof(buffer));
din >> buffer;
if(Translate(buffer)==N)
{
fout<< buffer << endl;
bFound = true;
/* cerr << "FOUND: " << buffer << endl;*/
}
/*if(bFound)
{
cerr << buffer << endl;
}*/
if(CompareStrings(buffer,maxname)>0) break;
}
if(!bFound) fout<< "NONE" << endl;
return 0;
}
All solutions