原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p2

问题描述:

Problem

The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B for instance, the program would press22. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ' ' should be printed to indicate a pause. For example, 2 2 indicates AA whereas 22 indicates B.

Input

The first line of input gives the number of cases, NN test cases follow. Each case is a line of text formatted as

   desired_message

Each message will consist of only lowercase characters a-z and space characters ' '. Pressing zero emits a space.

Output

For each test case, output one line containing "Case #x: " followed by the message translated into the sequence of keypresses.

Limits

1 ≤ N ≤ 100.

Small dataset

1 ≤ length of message in characters ≤ 15.

Large dataset

1 ≤ length of message in characters ≤ 1000.

Sample

Input 

hi
yes
foo bar
hello world Output
Case #1: 44 444
Case #2: 999337777
Case #3: 333666 6660 022 2777
Case #4: 4433555 555666096667775553

Perl算法:

  #!/usr/bin/perl
my $debug=; #该问题略微复杂,所以使用调试技术:如果$debug为1,则结果显示在标准输出上,而不输出到 .out 文件内;如果$debug 为0,则结果直接输出到 .out 文件中。
my $infile='C-large-practice.in';
my $outfile='C-large.out';
open $in,'<',$infile
or die "Cannot open $infile:$!\n";
open $out,'>',$outfile
or die "Cannot open $outfile:$!\n";
#建立哈希表
my %dict=(
=>"abc",
=>"def",
=>"ghi",
=>"jkl",
=>"mno",
=>"pqrs",
=>"tuv",
=>"wxyz",
=>" "
); if($debug){
for(keys %dict){
print "$_=>'$dict{$_}'\n";
}
}
chomp(my $N=<$in>);
my $str;
my $line;
my $prev,$cur;
$N= if $debug;
for($i=;$i<=$N;$i++){
$str="";
$prev="";
chomp($line=<$in>);
my $temp="";
print "===================== For \$line='$line' =======================:\n" if $debug;
print "length($line)=",length($line),"\n" if $debug;
for(my $index=;$index<length($line);$index++){
$cur=substr($line,$index,);
$temp .=$cur if $debug;
print "\$prev='$prev',\$cur='$cur',\$index='$index',\$temp='$temp'\n" if $debug;
#if($cur eq $prev){
# print "'$cur' eqs '$prev'\n" if $debug;
# $str .=" ";
#}#是否需要pause的关键不是 当前字符 $cur 与上一个字符 $prev 是否相等,而是当前需要按下的按键 $key 是否与上一个按键 $prev 是否相等,因此注释掉第一次使用的算法 LOOP1: while(($key,$value)=each %dict){
if((my $pos=index($value,"$cur"))!=-){
print "find '$cur' at $pos in '$value' for '$key'\n" if $debug;
if( $key eq $prev){ #是否需要pause的关键不是当前字符是否和上一个字符相等,而是当前需要按下的按键和上一个按键是否相等
print "'$key' eq '$prev' \n" if $debug;
$str .=" ";
}
$prev=$key;#将当前按键存储到上一个按键中,以便下一次比较
$str .= $key x ($pos+);
# last LOOP1; #即使已经找到了匹配的键值,也不能提前跳出循环,http://www.cnblogs.com/dongling/p/5705224.html 这篇随笔解释了原因
}
}
}
if($debug){
print "Case #$i: $str\n";
}
else{
print $out "Case #$i: $str\n";
}
}

上传原题地址链接网站,结果正确。

Google APAC----Africa 2010, Qualification Round(Problem C. T9 Spelling)----Perl 解法的更多相关文章

  1. Google APAC----Africa 2010, Qualification Round(Problem B. Reverse Words)----Perl 解法

    原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p1 问题描述: Problem Given a list of s ...

  2. Google APAC----Africa 2010, Qualification Round(Problem A. Store Credit)----Perl 解法

    原题地址链接:https://code.google.com/codejam/contest/351101/dashboard#s=p0 问题描述: Problem You receive a cre ...

  3. Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words

    Google Code Jam Africa 2010 Qualification Round Problem B. Reverse Words https://code.google.com/cod ...

  4. Google Code Jam Africa 2010 Qualification Round Problem A. Store Credit

    Google Code Jam Qualification Round Africa 2010 Problem A. Store Credit https://code.google.com/code ...

  5. Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam

    本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...

  6. Google Code Jam 2009 Qualification Round Problem B. Watersheds

    https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...

  7. Google Code Jam 2009 Qualification Round Problem A. Alien Language

    https://code.google.com/codejam/contest/90101/dashboard#s=p0 Problem After years of study, scientist ...

  8. [C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  9. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

随机推荐

  1. ES6之新增set数据结构

    学习set set是ES6中的新增的数据结构,是一种构造函数,类似于数组,但是里面的元素都是唯一的,不重复的. 例如: const set = new Set(); [1,2,3,4,5,2,1,2, ...

  2. 【Python】解析Python的标准数据类型

    目录结构: contents structure [-] 数值(Number) 数值类型 类型转化 Python中的Decimal数据类型 Python中的分数 Python中的算术方法 字符串(St ...

  3. 【笔记】AJAX+SweetAlert插件实现删除操作

    [笔记]AJAX+SweetAlert插件实现删除操作 Django AJAX SweetAlert  展示 SweetAlert 插件介绍 SweetAlert 是一个 JS 插件,能够完美替代 J ...

  4. 【App测试】:Monkey测试App稳定性

    一,前提搭建android studio的环境中: 二,CMD进入到AndroidSDK\platform-tools路径下:输入adb shell 这个提示就是表示手机未连接 三.连接安卓手机,手机 ...

  5. 【性能测试】:对WebSphere中间件的监控方式

    1  登录WebSphere控制台 2 选择应用.点击启动和停止来启动和停止应用 3 查看当前活动.点击要查看的项目 4 在开始监测前,先选中服务,点击启动监控将集合状态改为活动的 进入页面后,在页面 ...

  6. 利用scrapy-client发布爬虫的远程服务器

    一.环境准备 远程服务器必须装有scapyd,我们使用的机器必须有scrapy-client(我这里是windows),并确保这两者正常安装并启动. 二.客户端准备上传 首先进入到爬虫项目的根文件夹: ...

  7. 20190415 OSX系统安装和配置maven

    1.下载maven包[apache-maven-3.6.0-bin.zip] 2.解压到准备安装的目录,我的在:/Users/apple/Documents/javasource/apache-mav ...

  8. 第十篇--------javascript函数-参数

    javascript函数的参数:形参,实参 //function 参数 //形参列表 function test(a,b,c,d){ //alert(test.length); //形参个数,4个 / ...

  9. Oracle 12c 创建用户

    本文转载自 http://blog.itpub.net/29047826/viewspace-1453577/,侵删.   12c推出了可插拔库,管理用户也分为两种:本地用户和公用用户.本地用户是指在 ...

  10. 1-2 Mobx 入门实践之TodoList(官方Demo)

    第一步:导入模块 import React, { Component } from 'react'; import { observable, autorun,computed } from 'mob ...