Algorithm, Data structure/Solved Algorithmic Problem

USACO 1.2 - Name That Number

JaykayChoi 2016. 6. 6. 19:59

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



출처: http://train.usaco.org/



각각의 문자가 숫자와 매칭된다고 할 때 dict.txt 에서 주어진 숫자와 매칭되는 문자열을 출력하는 문제입니다.

multimap container에 미리 숫자로 모두 변환하여 담아두는 방법으로 풀어봤습니다.




my solving

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <fstream>
#include <iostream>
#include <string>
#include <map>
using namespace std;
 
string strArray = "ABCDEFGHIJKLMNOPRSTUVWXY";
multimap<long long, string> dict;
 
void readDict()
{
    ifstream fin("dict.txt");
    string str;
 
    while (fin >> str)
    {
        string strNum = "";
        for (int i = 0; i < str.length(); i++)
        {
            int temp = strArray.find(str[i]);
            temp /= 3;
            temp += 2;
            strNum += to_string(temp);
        }
        dict.insert(make_pair(stoll(strNum), str));
    }
}
 
 
int main() 
{
    ofstream fout("namenum.out");
    ifstream fin("namenum.in");
 
    long long number;
    fin >> number;
 
    readDict();
    bool isNone = true;
    pair< multimap<long long, string>::iterator, multimap<long long, string>::iterator > p = dict.equal_range(number);
    for (multimap<long long, string>::iterator it = p.first; it != p.second; it++)
    {
        isNone = false;
        fout << it->second << endl;
    }
    
    if (isNone)
        fout << "NONE" << endl;
 
    return 0;
}
cs


'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글

USACO 1.2 - Dual Palindromes  (0) 2016.06.08
USACO 1.2 - Palindromic Squares  (0) 2016.06.08
USACO 1.2 - Transformations  (0) 2016.06.06
USACO 1.2 - Milking Cows  (0) 2016.06.05
USACO 1.1 - Broken Necklace  (0) 2016.06.01