[Cogs & usaco training] 710. Name the number (hash + water question + DFS)

Source: Internet
Author: User

Http://cojs.tk/cogs/problem/problem.php? PID = 1, 710

Recently, water flooding has started...

In order to practice hash... However, hash runs slower than brute force ..

It is self-evident...

#include <cstdio>#include <cstring>#include <cmath>#include <string>#include <iostream>#include <algorithm>#include <string>using namespace std;#define rep(i, n) for(int i=0; i<(n); ++i)#define for1(i,a,n) for(int i=(a);i<=(n);++i)#define for2(i,a,n) for(int i=(a);i<(n);++i)#define for3(i,a,n) for(int i=(a);i>=(n);--i)#define for4(i,a,n) for(int i=(a);i>(n);--i)#define CC(i,a) memset(i,a,sizeof(i))#define read(a) a=getint()#define print(a) printf("%d", a)#define dbg(x) cout << #x << " = " << x << endl#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }#define setio(x) freopen(""#x".in", "r", stdin); freopen(""#x".out", "w", stdout)inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<‘0‘||c>‘9‘; c=getchar()) if(c==‘-‘) k=-1; for(; c>=‘0‘&&c<=‘9‘; c=getchar()) r=r*10+c-‘0‘; return k*r; }inline const int max(const int &a, const int &b) { return a>b?a:b; }inline const int min(const int &a, const int &b) { return a<b?a:b; }using namespace std;struct Hash {char s[30];Hash() { s[0]=‘ ‘; }}vis[100008];char s[30], t[30];int len, flag;inline const void sethash(char *str) {int l=strlen(str), ret=0, k=1;rep(i, l) ret=k*(str[i]-‘A‘)%100007, k=(k*27)%100007;while(vis[ret].s[0]!=‘ ‘) { ++ret; if(ret==100007) ret=0; }strcpy(vis[ret].s, str);}inline const bool check(char *str) {int l=strlen(str), ret=0, k=1;rep(i, l) ret=k*(str[i]-‘A‘)%100007, k=(k*27)%100007;while(vis[ret].s[0]!=‘ ‘) { if(!strcmp(vis[ret].s, str)) return 1;++ret; if(ret==100007) ret=0;}return 0;}const int mp[10][3]={{ 0,   0,   0}, { 0,   0,   0}, {‘A‘, ‘B‘, ‘C‘}, {‘D‘, ‘E‘, ‘F‘}, {‘G‘, ‘H‘, ‘I‘},{‘J‘, ‘K‘, ‘L‘}, {‘M‘, ‘N‘, ‘O‘}, {‘P‘, ‘R‘, ‘S‘},{‘T‘, ‘U‘, ‘V‘}, {‘W‘, ‘X‘, ‘Y‘}};void dfs(const int &x) {if(x==len) { if(check(t)) { printf("%s\n", t); flag=1; } return; }int num=s[x]-‘0‘;rep(i, 3) {t[x]=(char)mp[num][i];dfs(x+1);}}int main() {char str[30];setio(namenum);rep(i, 4617) {scanf("%s", str);sethash(str);}scanf("%s", s);len=strlen(s);dfs(0);if(!flag) puts("NONE");return 0;}

 

 

 

 

Name the number

Translated by timgreen

Among the cattle farm operators in Wisconsin, the accounting department is used to branding cows with continuous numbers.
However, when cows use mobile phones, they do not feel the convenience of this system. They prefer to call their companions with their favorite names, rather than using the statement "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 received data from the number to the text, except for "Q" and "z" (not shown)

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 placed in the first few rows of test data, which contains a series of less than 5,000 acceptable ox names. (All names are in uppercase)
After receiving the number of the cow, return the names that can be translated from the number and in the dictionary.
For example, number 4734 can generate 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

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

Challenge one
Write a program to print all valid names for the given number. If not, output "NONE ''.
The number may contain 12 digits.
Program name: namenum

Input Format
The first 4618 rows are acceptable names (that is, dictionaries), and the last separate row contains a number (the length may be from 1 to 12 ).

 

Sample input (File namenum. In)

/* The Dictionary of the first several rows is omitted */

4734


Output Format
Output A list of valid names in alphabetical order, with one name per row.

Sample output (File namenum. out)
Greg

[Cogs & usaco training] 710. Name the number (hash + water question + DFS)

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.