Usaco/name that number (enumeration, hash)

Source: Internet
Author: User
Translate: usaco/namenum

Name that numberName the number

Among the cattle farm operators in Wisconsin, the accounting department is used to branding cows with continuous numbers. However, the cows themselves do not feel the convenience of this system. They prefer to call their companions with their favorite names, rather than using statements like this "C' mon, #4734, get along. ". Please write a program to help the poor wrangler translate the brand number of a cow into a possible name. Because the cows now have mobile phones, use the standard button layout to translate the number into text :(
Apart from "Q" and "z") Descriptions
        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 are put in such a file called "dict.txt", which contains a series of numbers of less than 5,000 (accurately 4617) Acceptable ox names. (All names are in uppercase and are alphabetically arranged.) read the numbers of the cows and return the names that can be translated from the numbers and in the dictionary. For example, 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 gbsfg hybrid hpeg hybrid hpei hpfg HPFH hrdg HRDH hrdi HREG hybrid hsdh hsdi 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

It happens that only one of 81 "Greg" is valid (in the dictionary ).

Write a program to print all valid names for the given number. If not, output none. The number may contain 12 digits.


Format

Program name: Namenum

Input Format:

(File namenum. In)

A separate row contains a number (the length may be from 1 to 12 ).

(File dict.txt)

Http://ace.delos.com/usaco/namenumdict.txt

Output Format:

(File namenum. out)

Output A list of valid names in alphabetical order, with one name per row.If no valid name exists, 'none' is output '.


Sample Input
4734 

Sample output
GREG
Analysis

A number corresponds to three letters. If we enumerate all the strings represented by a number, there will be 3 ^ 12 types of strings, and then we can look for them in the 5000 dictionary. We can use binary lookup, the data size is 3 ^ 12 * log5000 = 6.5e6, and the space size is 5000.

Actually, it can do better!

A letter corresponds to only one number. It reads a word from the dictionary and converts it into a unique number to see if it matches the given number, the time scale is 5000*12 = 6e4, the space scale is constant, and the programming complexity is low.

You can also compare the length of characters and numbers first. If they are equal, compare them by bit.

I will give a formula for converting letters into numbers to prove that they are not written. It is easy to understand: temp: = ord (Nam [I] [J])-64; if temp> 17 then Dec (temp); num [I]: = num [I] * 10 + (temp-1) Div 3) + 2; temp is the value after the J-character ASCII code-64 of the I-th name, And num [I] is the number converted from the I-th name. ---- By tonyshaw.

There is another way

Use the hash table to hash the dictionary. Then, search for the new string in the hash table and add it to the output list if it is found. Finally, the output is sorted quickly and then output.

/*ID: 138_3531LANG: C++TASK: namenum*/
#include<iostream>#include<fstream>#include<cstring>using namespace std;const char code[26]={'2','2','2','3','3','3','4','4','4','5','5','5','6','6','6','7','0','7','7','8','8','8','9','9','9','0'};char a[13],b[13];int main(){  freopen("namenum.in","r",stdin);  freopen("namenum.out","w",stdout);  freopen("dict.txt","r",stderr);  bool flag=1;  cin>>a;  while (fscanf(stderr,"%s",b)!=EOF)    {      bool flg=1;      if (strlen(a)!=strlen(b)) continue;      for (int i=0;b[i];i++) flg=flg&&(code[b[i]-'A']==a[i]);      if (flg) flag=0,cout<<b<<endl;    }  if (flag) cout<<"NONE"<<endl;  return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.