USCAO section 1.2 Name That Number

【UGUI】二、Text(TMP) 文本 Text(TMP)、导入中文字库 阅读详情

 

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

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

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

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

所以也懒得去删除了。

/*
ID:nealgav1
PROG:namenum
LANG:C++
*/
#include<cstdio>
#include<cstring>
#define N 12345
FILE *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: namenum
LANG: C++

Compiling...
Compile: OK

Executing...
   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 your submission #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 string without 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;
}

 

TSP、MTSP问题遗传算法详细解读及python实现 写在前面 遗传算法是一种求解NPC问题的启发式算法,属于仿生进化算法族的一员。仿生进化算法是受生物行为启发而发明的智能优化算法,往往是人们发现某种生物的个体虽然行为较为简单,但生物集群通过某种原理却能表现出智能行为。于是不同的人研究不同的生物行为原理,受到启发而发明出新的仿生进化算法。比如免疫优化算法,蚁群算法,模拟退火算法等,这些算法以后也会简单介绍。 本文的主题是遗传算法,该算法也是受到生物行为启发。物竞天择,适者生存,优胜劣汰,是该优化算法的核心思想。笔者在业务中需要用到遗传算法求解TSP问题,但是网 阅读详情

相关推荐

Oracle EBS R12 AP模块发票导入接口实战:从SQL插入到API调用的完整流程

本文详细解析了Oracle EBS R12 AP模块发票导入接口的完整流程,从SQL插入到API调用的实战技巧。涵盖接口表设计、数据验证、双通道导入机制及错误诊断,特别分享官方文档未提及的高阶优化方案,助力企业实现财务数字化转型。

weixin_33728268的博客 476

USCAO-Section 1.2 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

qq_34407173的博客 319

全国职业院校技能大赛-大数据应用赛项-数据挖掘-01

-----------------------推荐Top5结果如下--------------------------------------------第一条数据前10列结果展示为:----------------------------------------相同种类前10的id结果展示为:--------------------相似度top5(商品id:14,平均相似度:0.522356)相似度top1(商品id:1,平均相似度:0.983456)若属于该spu_id,则内容为1否则为0。

Eternity_04的博客 1953

USCAO section 1.4 Number Triangles(DP)

Number TrianglesConsider the number triangle shown below. Write a program that calculates the highest sum of numbers that can be passed on a route that starts at the top and ends somewhere on the base

nealgavin的专栏 566

PCIe扫盲(三)

需要特别注意的是,PCIe的Spec中明确规定只有Root有权限发起配置请求(Originate Configuration Requests),也就是说PCIe系统里面的其他设备是不允许去配置其他设备的配置空间的,即peer-to-peer的配置请求是不允许的。当配置请求进行BDF路由的时候,正是依靠这些信息来确定要找的设备的。前面的文章还介绍过,Root和Switch的每一个端口中都包含一个P2P桥,并且知道桥的配置空间头(Configuration Space Header)是Type1型的。

Liuqz2009的专栏 2402

Qt6导入QtChart绘制图表(添加组件)

我在按照大佬们的说明添加QtChart时发现,我的电脑上并不能正确的添加QtChart并编译通过,所以在综合了两位大佬的说明又自己探索了一番后,正确编译通过,分享出来,以便同志们节省一些时间。

Nuaa0719301zyx的博客 2950

USCAO section1.2 Palindromic Squares

Palindromic Squares Rob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 Print both the number and its squ

nealgavin的专栏 627

uniapp H5刷新页面后返回按钮失效问题完美解决方案

这个问题其实很隐蔽,尤其是uniapp H5 场景下刷新导致栈清理,很多开发者一开始都懵掉了。uniapp 内部对「页面栈只有一个」时的返回逻辑是reLaunch;所以只要在这个分支里替换为自己的逻辑(比如),就能顺利实现预期。目前我在项目里已经跑通了,暂时没发现问题。不得不说,知道源码里这个逻辑后,真的是醍醐灌顶,只能膜拜框架大佬了 🤯。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 523

USCAO section 1.3 Calf Flac

Calf Flac It is said that if you give an infinite number of cows an infinite number of heavy-duty laptops (with very large keys), that they will ultimately produce all the world's great palindromes

nealgavin的专栏 6656

Oracle EBS Tax 四国实现详解:印度、巴西、美国、阿根廷

印度:务必启用India Localization模块,不要尝试用标准EBTax模拟GST逻辑 巴西:必须配置NF-e接口,建议聘请本地税务顾问设置CFOP和NCM代码映射 美国:跨州业务强烈建议购买Vertex或Avalara服务,自建税率表维护成本极高 阿根廷:注意1:1:1架构限制,提前规划Legal Entity和OU结构

金牌架构师 260

AI Agent在智能腰带中的姿势矫正与提醒

随着人们生活方式的改变,长时间不正确的坐姿、站姿等不良姿势问题日益普遍,这不仅会导致身体疲劳,还可能引发颈椎病、腰椎病等多种健康问题。智能腰带作为一种可穿戴设备,具有实时监测人体姿势的潜力。本研究的目的是将AI Agent引入智能腰带,实现对人体姿势的准确识别、矫正与提醒功能。本研究的范围涵盖了从AI Agent的核心算法原理到智能腰带的实际开发与应用。包括传感器数据的采集与处理、机器学习算法的选择与训练、智能腰带的硬件设计与软件开发,以及在不同场景下的实际应用测试。本文共分为十个部分。

AI 原生应用开发的博客 344

USCAO Section 1.1 Milking Cows

Milking Cows Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time

nealgavin的专栏 747

【深度学习新浪潮】算力上太空:NVIDIA Space-1 Vera Rubin Module 深度技术解析

NVIDIA Space-1 Vera Rubin Module 不只是一款芯片模组,它是天地一体化 AI 的基础设施底座。它用成熟的 GPU 架构 + 航天级加固,把地面 AI 生态直接平移到太空。下一个 AI 创新战场,不在云端,而在轨道。在轨 LLM星上多模态实时处理卫星自主组网与分布式推理太空算力成为云计算的一级节点参考资料NVIDIA 官方博客:NVIDIA 推出太空计算,将 AI 送入太空轨道GTC 2026 黄仁勋主题演讲。

专注:太空算力、AI infra、异构调度、大模型成本工程 1125

USCAO section 1.3 Mixing Milk

Mixing Milk Since milk packaging is such a low margin business, it is important to keep the price of the raw product (milk) as low as possible. Help Merry Milk Makers get the milk they need in the

nealgavin的专栏 568

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

USCAO section 1.2 Dual Palindromes

Dual Palindromes Mario Cruz (Colombia) & Hugo Rickeboer (Argentina) A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palind

nealgavin的专栏 529

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

USCAO section 1.1 Transformations

Transformations A square pattern of size N x N (1 #1: 90 Degree Rotation: The pattern was rotated clockwise 90 degrees. #2: 180 Degree Rotation: The pattern was rotated clockwise 180 degrees. #3: 270

nealgavin的专栏 544

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

USCAO section1.3 Barn Repair

Barn RepairIt was a dark and stormy night that ripped the roof and gates off the stalls that hold Farmer John's cows. Happily, many of the cows were on vacation, so the barn was not completely full. T

nealgavin的专栏 742

USCAO section 1.4 The Clocks

The Clocks IOI'94 - Day 2 Consider nine clocks arranged in a 3x3 array thusly: |-------| |-------| |-------| | | | | | | | |---O | |---O | | O |

nealgavin的专栏 564

USCAO section 1.4 Packing Rectangles

Packing Rectangles IOI 95 The six basic layouts of four rectangles Four rectangles are given. Find the smallest enclosing (new) rectangle into which these four may be fitted without overlapping. B

nealgavin的专栏 615

USCAO section Mother's Milk(搜索)

Mother's Milk Farmer John has three milking buckets of capacity A, B, and C liters. Each of the numbers A, B, and C is an integer from 1 through 20, inclusive. Initially, buckets A and B are empty

nealgavin的专栏 847
上一篇: USCAO section 1.2 Dual Palindromes
下一篇: USCAO section 1.3 Mixing Milk
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值