USCAO section 1.2 Name That Number

来源:互联网 发布:jsp后台管理系统源码 编辑:程序博客网 时间:2024/05/19 00:37

 

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 GRDIGREG GREH GREI GRFG GRFH GRFI GSDG GSDH GSDI GSEG GSEH GSEIGSFG GSFH GSFI HPDG HPDH HPDI HPEG HPEH HPEI HPFG HPFH HPFIHRDG HRDH HRDI HREG HREH HREI HRFG HRFH HRFI HSDG HSDH HSDIHSEG HSEH HSEI HSFG HSFH HSFI IPDG IPDH IPDI IPEG IPEH IPEIIPFG IPFH IPFI IRDG IRDH IRDI IREG IREH IREI IRFG IRFH IRFIISDG 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

这一题着实A的不容易啊,原先题意理解错了以为只要是单词里面含有就行结果悲剧了。

这样题的思路是将dict.txt里面的单词化成数字和读入的数比较,成功则输出。

程序里有个字符串拷贝比较,是用来避免出现相同单词的情况,其实字典序是不用考虑这个的,只是原先理解错了题意才需要的

所以也懒得去删除了。

/*ID:nealgav1PROG:namenumLANG:C++*/#include<cstdio>#include<cstring>#define N 12345FILE *fin;FILE *kin;const int ex[]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,-110,7,7,8,8,8,9,9,9,-111,-111,-111,-111,-111,-111};int num[80];char lina[80];char ken[80];int main(){  freopen("namenum.in","r",stdin);  freopen("namenum.out","w",stdout);  int i=0;  int m;  bool ans;  char flag[80];  while(scanf("%s",lina)!=EOF)  { fin=fopen("dict.txt","r");    ans=0;    strcpy(flag,"123");    int k=strlen(lina);    for(i=0;i<k;i++)    num[i]=lina[i]-'0';    num[i]=0;//哨点     while(fscanf(fin,"%s",&ken)!=EOF)    {      int j=0;      while(ken[j]!='\0')      {        if(ex[ken[j]-'A']!=num[j])        {          break;        }        j++;      }      if(num[j]==0&&ken[j]=='\0')      {        if(strcmp(flag,ken)!=0)        {          for(int k=0;k<j;k++)         printf("%c",ken[k]);         printf("\n");         ans=1;        }        strcpy(flag,ken);      }    }    if(!ans)    printf("NONE\n");  }}


 

 

USER: Neal Gavin Gavin [nealgav1]TASK: namenumLANG: C++Compiling...Compile: OKExecuting...   Test 1: TEST OK [0.000 secs, 3348 KB]   Test 2: TEST OK [0.000 secs, 3348 KB]   Test 3: TEST OK [0.000 secs, 3348 KB]   Test 4: TEST OK [0.000 secs, 3348 KB]   Test 5: TEST OK [0.000 secs, 3348 KB]   Test 6: TEST OK [0.000 secs, 3348 KB]   Test 7: TEST OK [0.000 secs, 3348 KB]   Test 8: TEST OK [0.000 secs, 3348 KB]   Test 9: TEST OK [0.000 secs, 3348 KB]   Test 10: TEST OK [0.000 secs, 3348 KB]   Test 11: TEST OK [0.000 secs, 3348 KB]   Test 12: TEST OK [0.000 secs, 3348 KB]   Test 13: TEST OK [0.000 secs, 3348 KB]   Test 14: TEST OK [0.000 secs, 3348 KB]   Test 15: TEST OK [0.000 secs, 3348 KB]All tests OK.

Your program ('namenum') produced all correct answers! This is yoursubmission #6 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----4734------- test 2 ----234643------- test 3 ----5747867437------- test 4 ----223------- test 5 ----532------- test 6 ----546------- test 7 ----53662------- test 8 ----5455426------- test 9 ----26678268463------- test 10 ----463373633623------- test 11 ----282742662------- test 12 ----463373633623------- test 13 ----2336------- test 14 ----5264------- test 15 ----426
Keep up the good work!

Thanks for your submission

 

 

Name That NumberRuss Cox & Rob Kolstad

There are two ways to do this problem. One is, given the number, to generate all possible strings that encode to that number and look them up in the dictionary. Since there are 3 letters for each number and 12 digits in the string, that's 312 = 531441 lookups into a dictionary of size 5000, which although manageable would be a little on the long side (binary search can help this).

Or, we can examine each word in the dictionary to see if it maps to the digits of the number in question. This has the the advantage of a shorter program that probably will work right first time.


Here is Argentina competitor's Michel Mizrah's solution using the first method with a binary search. While it is blazingly fast, it does have the disadvantage of some fairly tricky coding in the binary search routine. A single off-by-one error would doom a program in a contest.

#include <stdio.h>#include <stdlib.h>#include <string.h>char num[12],sol[12];char dict[5000][13];int nsolutions = 0;int nwords;int maxlen;FILE *out;void calc (int charloc, int low, int high) {    if (charloc == maxlen) {        sol[charloc] = '\0';        for (int x = low; x < high; x++) {            if (strcmp (sol, dict[x]) == 0) {                fprintf (out, "%s\n", sol);                nsolutions++;            }        }        return;   }   if (charloc > 0) {        for (int j=low; j <= high; j++){            if (sol[charloc-1] == dict[j][charloc-1]) {                low=j;                while (sol[charloc-1] == dict[j][charloc-1])                    j++;                high=j;                break;            }            if (j == high) return;        }    }    if (low > high) return;    switch(num[charloc]){      case '2':sol[charloc] = 'A'; calc(charloc+1,low,high);               sol[charloc] = 'B'; calc(charloc+1,low,high);               sol[charloc] = 'C'; calc(charloc+1,low,high);               break;       case '3':sol[charloc] = 'D'; calc(charloc+1,low,high);               sol[charloc] = 'E'; calc(charloc+1,low,high);               sol[charloc] = 'F'; calc(charloc+1,low,high);               break;       case '4':sol[charloc] = 'G'; calc(charloc+1,low,high);               sol[charloc] = 'H'; calc(charloc+1,low,high);               sol[charloc] = 'I'; calc(charloc+1,low,high);               break;       case '5':sol[charloc] = 'J'; calc(charloc+1,low,high);               sol[charloc] = 'K'; calc(charloc+1,low,high);               sol[charloc] = 'L'; calc(charloc+1,low,high);               break;       case '6':sol[charloc] = 'M'; calc(charloc+1,low,high);               sol[charloc] = 'N'; calc(charloc+1,low,high);               sol[charloc] = 'O'; calc(charloc+1,low,high);               break;       case '7':sol[charloc] = 'P'; calc(charloc+1,low,high);               sol[charloc] = 'R'; calc(charloc+1,low,high);               sol[charloc] = 'S'; calc(charloc+1,low,high);               break;       case '8':sol[charloc] = 'T'; calc(charloc+1,low,high);               sol[charloc] = 'U'; calc(charloc+1,low,high);               sol[charloc] = 'V'; calc(charloc+1,low,high);               break;       case '9':sol[charloc] = 'W'; calc(charloc+1,low,high);               sol[charloc] = 'X'; calc(charloc+1,low,high);               sol[charloc] = 'Y'; calc(charloc+1,low,high);               break;   }}int main(){    FILE *in=fopen ("namenum.in", "r");    FILE *in2=fopen ("dict.txt", "r");    int j;    out=fopen ("namenum.out","w");    for (nwords = 0; fscanf (in2, "%s", &dict[nwords++]) != EOF; )        ;    fscanf (in, "%s",&num);    maxlen = strlen(num);    calc (0, 0, nwords);    if (nsolutions == 0) fprintf(out,"NONE\n");    return 0;}

The solution below might be considered to be a bit more straightforward: no tricky offsets, no +1 or -1, no knowledge about character values. The lines of actual code in this solution are minimal.

This is the sort of program that might work reliably the first time and every time. The only tricky part is knowing that scanf will yield stringwithout a newline on the end:

#include <stdio.h>#include <stdlib.h>#include <string.h>int main() {    FILE *in = fopen ("namenum.in", "r");    FILE *in2 = fopen ("dict.txt", "r");    FILE *out = fopen ("namenum.out","w");    int nsolutions = 0;    int numlen;    char word[80], num[80], *p, *q, map[256];    int i, j;    map['A'] = map['B'] = map['C'] = '2';    map['D'] = map['E'] = map['F'] = '3';    map['G'] = map['H'] = map['I'] = '4';    map['J'] = map['K'] = map['L'] = '5';    map['M'] = map['N'] = map['O'] = '6';    map['P'] = map['R'] = map['S'] = '7';    map['T'] = map['U'] = map['V'] = '8';    map['W'] = map['X'] = map['Y'] = '9';    fscanf (in, "%s",num);    numlen = strlen(num);    while (fscanf (in2, "%s", word) != EOF) {        for (p=word, q=num; *p && *q; p++, q++) {            if (map[*p] != *q)                break;        }        if (*p == '\0' && *q == '\0') {            fprintf (out, "%s\n", word);            nsolutions++;        }    }    if (nsolutions == 0) fprintf(out,"NONE\n");    return 0;}

 

原创粉丝点击