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

Source Code code format="java" /* ID: wjh3331 LANG: JAVA TASK: namenum */

import java.io.*;

public class namenum { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("namenum.in")); PrintWriter out = new PrintWriter(new FileWriter("namenum.out")); BufferedReader dict = new BufferedReader(new FileReader("dict.txt")); // 이름 데이터

char map[] = new char['Z' - 'A' + 1]; // 문자를 숫자로 매핑하는 배열

// 매핑 설정 map['A' - 'A'] = map['B' - 'A'] = map['C' - 'A'] = '2'; map['D' - 'A'] = map['E' - 'A'] = map['F' - 'A'] = '3'; map['G' - 'A'] = map['H' - 'A'] = map['I' - 'A'] = '4'; map['J' - 'A'] = map['K' - 'A'] = map['L' - 'A'] = '5'; map['M' - 'A'] = map['N' - 'A'] = map['O' - 'A'] = '6'; map['P' - 'A'] = map['R' - 'A'] = map['S' - 'A'] = '7'; map['T' - 'A'] = map['U' - 'A'] = map['V' - 'A'] = '8'; map['W' - 'A'] = map['X' - 'A'] = map['Y' - 'A'] = '9';

String num = in.readLine; char n = num.charAt(0); // 숫자의 첫자리

boolean match, matched = false; // 일치여부, 일치된 적이 있는지 여부 String name; // 이름 while((name = dict.readLine) != null) { char m = map[name.charAt(0) - 'A']; // 이름의 첫번째 글자를 매핑

// 숫자보다 작으면 패스 if(m < n) { continue; }     // 첫 자리가 같으면 else if(m == n) { // 길이가 다르면 패스 if(name.length != num.length) { continue; }       // 일치 여부 판단 match = true; for(int i = 1; i < name.length; i++) { if(map[name.charAt(i) - 'A'] != num.charAt(i)) { match = false; break; }       }        // 일치하면 출력 if(match) { out.println(name); matched = true; }

}     // 문자가 숫자보다 크면 종료하기위해 탈출 else { break; }   }

// 일치된게 없으면 없다고 출력 if(!matched) { out.println("NONE"); }

out.close; System.exit(0); } } code

Comment USACO는 저지 서버가 성능이 좋은 듯... 허접하게 풀어도 다 통과하네요... 문제를 풀고 한지연님 해답을 보니 G.G... 그래서 저도 범위를 벗어나면 종료하게 수정하였습니다.(감솨+_+) 정렬이 되어있길래 바이너리 서치를 해보려고 했는데... RandomAccessFile 클래스는 바이트 단위로 접근하더군요;;