USACO : Name That Number


/*
User ID: tanmoyt1
Link : http://train.usaco.org/usacoprob2?a=z9TmPNj3S8H&S=namenum
*/

/*
PROG: namenum
LANG: C++
*/

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

using namespace std;

string serializeName(const string &in)
{
    string ret = "";
    for(int i = 0; i < in.length(); ++i)
    {
        if(in[i] == 'Q' || in[i] == 'Z')
            return "1";
        if(in[i] < 'Q')
            ret += ((in[i] - 'A') / 3) + '2';
        else
            ret += ((in[i] - 'Q') / 3) + '7';
    }
    return ret;
}

int main()
{
     freopen("namenum.in","r",stdin);
     freopen("namenum.out","w",stdout);
    ifstream fdict ("dict.txt");

    string serial;
    cin >> serial;

    bool found = false;
    string entry;
    while(fdict >> entry)
        if(entry.length() == serial.length() && serializeName(entry) == serial)
        {
            found = true;
            cout << entry << endl;
        }

    if(!found)
        cout << "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