【USACO1.2.3】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

老规矩直接上翻译地址 http://www.nocow.cn/index.php/Translate:USACO/namenum

通过相应数字转化为字母 并在dict.txt中查找相应单词匹配

我是直接在dict中搜索 每个单词转化为相应的数字 并进行匹配 简单的字符串处理 因为数据范围不大 所以感觉效率还好。。。

dict文件的处理不会 捂脸 现场百度学习的。。orz

/*
ID: Augur Alpha
LANG: C++
TASK: namenum
*/
#include <iostream>
#include <cstdio>
#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 str[13], temp[13], cnt[13];

int main(){
    freopen("namenum.in", "r", stdin);
    freopen("namenum.out", "w", stdout);
    freopen("dict.txt", "r", stderr);
    bool flag = false;
    gets(str);
    while(fscanf(stderr, "%s", temp)!=EOF){
        if(code[temp[0]-'A'] != str[0]){
            continue;
        }

        int len = strlen(temp);
        for(int i = 0; i < len; ++i){
            cnt[i] = code[temp[i]-'A'];
        }
        cnt[len] = '\0';

        if(!strcmp(str, cnt)){
            flag = true;
            puts(temp);
        }
    }
    if(!flag){
        puts("NONE");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-08 09:21:51

【USACO1.2.3】Name That Number(字符串处理)的相关文章

leetCode 179. Largest Number 字符串排序 | Medium

179. Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to return a st

leetCode 17. Letter Combinations of a Phone Number 字符串 | 回溯 | Medium

17. Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23"Ou

Leetcode 171 Excel Sheet Column Number 字符串处理

本质是把26进制转化为10进制 A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 1 class Solution { 2 public: 3 int titleToNumber(string s) { 4 int ans = 0; 5 for(int i = 0; i<s.size(); ++i){ 6 ans = 26 * ans + s[i] - 'A' + 1; 7 } 8 return ans; 9 }

python--JavaScript(js)/上

JavaScript(js) ECMA-----定义的基础语法 DOM------document  object  model BOM------Browser  object  model Javasript 基于对象的,也是面向对象 ECMAScript描述了以下内容: 语法 类型 语句 关键字 保留字 运算符 对象(封装 继承 多态)基于对象的语言 使用对象 JavaScript的引入方式 直接编写: <script> alert(123) </script> 导入文件 &

typeof的用法

经常会在js里用到数组,比如 多个名字相同的input, 若是动态生成的, 提交时就需要判断其是否是数组. if(document.mylist.length != "undefined" ) {} 这个用法有误. 正确的是 if( typeof(document.mylist.length) != "undefined" ) {} 或 if( !isNaN(document.mylist.length) ) {} typeof的运算数未定义,返回的就是 "

js①

JavaScript的引入方式 直接在script标签内部书写代码 ```html <!DOCTYPE html> ``` 2. 通过script标签的src属性,引入外部的JavaScript文件 ```html <!DOCTYPE html> ``` 数据类型 在JavaScript中一种有5种原始类型 - 数值类型(number) - 字符串类型(string) - 布尔值类型(boolean) - null - undefined 变量的命名规则 命名规则: - 第一个字符

JavaScript三种数据类型之间的互转

一:number<===>string  数字类型和字符串类型之间的互相转换 number===>string 数字转字符串有三种方式: 1.在数字后面 +“ ”; 2.利用字符串的包装类型 String(number); 3.类型Object的prototype原型中的 toString()方法; var num=10; var str1=num+""; var str2=String(num); var str3=num.toString(); document.

【转】Java 多线程(四) 多线程访问成员变量与局部变量

原文网址:http://www.cnblogs.com/mengdd/archive/2013/02/16/2913659.html 先看一个程序例子: public class HelloThreadTest { public static void main(String[] args) { HelloThread r = new HelloThread(); Thread t1 = new Thread(r); Thread t2 = new Thread(r); t1.start();

js语法概述

# 语法概述 ## JavaScript的引入方式 1. 直接在`script`标签内部书写代码 ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script type="text/javascript"> console.log('hello, world'); </s

JavaScript基础重点

接触JavaScript这门语言也就很长的时间了,但从来没有系统的去了解这么语言.趁现在刚刚毕业以及某些原因无心工作的情况下去系统的了解一下这么语言,也想通过这么语言养成写博客的习惯,因为我认为这是一件对程序员来说很神圣又很光荣的事情. 1.1背景 相信很多初学者都遗忘或混淆的就是JavaScript的官方命名:ECMAScript.2015年6月17日,ECMAScript 6发布正式版本,即ECMAScript 2015. 1.2语法 常规语法省略 重点强调: 1.原始值和对象:原始值包括布