原题地址链接: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. python全栈开发_day17_时间,系统模板和序列化

    一:时间模板 1)time 常用功能: time.sleep() time.time() time.strftime() import time print(time.strftime("% ...

  2. HTML常用汇总

    HTML注释 <!-- --> XHTML:就是要遵守XML规则的HTML标签 DHTML:包含html,js,等动态HTML 表单元素提交时提交的是name属性 get提交.post提交 ...

  3. win2003设置单用户登录

    远程桌面是windows操作系统中一个很方便的功能,管理测试机资产.异地排除故障等,都很快捷.在windows xp sp2模式下,一般默认是单用户登录,也就是当A用户远程一台机器时,B用户在远程到这 ...

  4. OPENERP 新添模块后导致启动不了的问题总结

    自己新的的模块在本地运行OK,上传到服务器后,导致所有用户登录不了,并报“ProgrammingError: column “” does not exist”不存在的错误. 错误原因,自定义模块发生 ...

  5. normalize.css源码解析

    什么是normalize.css?  它是为了帮助我们统一各个浏览器的样式和消除bug的css库. 为什么需要normalize.css,有什么好处? 不像一些reset.css,normalize. ...

  6. 爬虫URL去重

    这个要看你想抓取的网页数量是哪种规模的.如果是千万以下用hash表, set, 布隆过滤器基本可以解决,如果是海量的......嗯我也没做过海量的,不过hash表之类的就别想了,内存根本不够,分割线下 ...

  7. sql中COUNT(*)、COUNT(字段名)的区别

    数据表:其中IT002的Fname是null. 执行sql: ) FROM T_Employee 结果: 结论:COUNT(*)统计的是结果集的总条数,而COUNT(FName)统计的则是除了结果集中 ...

  8. JavaScript设计模式-7.单例模式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. JS中深拷贝数组、对象、对象数组方法

    我们在JS程序中需要进行频繁的变量赋值运算,对于字符串.布尔值等可直接使用赋值运算符 “=” 即可,但是对于数组.对象.对象数组的拷贝,我们需要理解更多的内容. 首先,我们需要了解JS的浅拷贝与深拷贝 ...

  10. Comet技术在Java Web中的应用

    HTTP协议是互联网上大量信息交换的基础,其特点是,它是基于请求-响应模式的无状态的单向协议,即,必须由客户端发起一个请求建立连接,服务器接收请 求,把数据返回给客户端,然后释放连接.下一次,再由客户 ...