Google APAC----Africa 2010, Qualification Round(Problem C. T9 Spelling)----Perl 解法
原题地址链接: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, N. N 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 解法的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Google Code Jam 2009 Qualification Round Problem B. Watersheds
https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...
- 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 ...
- [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) ...
- [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) ...
随机推荐
- QuantLib 金融计算——数学工具之插值
目录 QuantLib 金融计算--数学工具之插值 概述 一维插值方法 二维插值方法 如果未做特别说明,文中的程序都是 Python3 代码. QuantLib 金融计算--数学工具之插值 载入模块 ...
- TX2 刷机时遇到Parsing board information failed
因为之前调试I2C时,修改了EEPROM Layout,所以,在刷机时遇到此问题. 解决办法是按照此文档中的介绍来修改布局. 实际操作时,我拿了一块正常的TX2,按照指令: sudo i2cdump ...
- shell (三) 文件压缩
查看压缩文件 #tar tvf tar.tar.gz -rw-r--r-- root/root 290 2019-03-22 14:38 README.md -rw-r--r-- root/root ...
- vue构造器以及实例属性
一.构造器 1.vue.js就是一个构造器,通过构造器Vue来实例化一个对象:例如:var vm = new Vue({}); 2.实例化Vue时,需要传入一个参数(选项对象): 3.参数:选项对象可 ...
- 各种height/width总结
CSS盒模型是比较复杂的,尤其是当页面中有滚动条时,仅仅通过css来操作高度宽度是不够的,幸运的是Javascript提供了不少这样的接口.Javascript中clientHeight / clie ...
- 《LeetBook》leetcode题解(6): ZigZag Conversion[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- javac之向前引用
可以参考JLS7:https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.3.2.3 public class Test5 ...
- Java类文件结构及javac的ClassReader类解读
首先来看一下ClassFile,类注释如下: A JVM class file. Generic Java classfiles have one additional attribute for c ...
- Struts dispatchAction
在Struts中定义动态Action,不用定义多个Action,可以实现一个action,多个跳转. 在定义时,继承DispatchAction,并定义parameter的名字 在jsp页面选择act ...
- mysql8安装配置备忘
Mysql8安装配置 1. 初始化 进入mysql安装目录中的bin目录,在cmd中输入: mysqld --initialize --console 加上--console参数可以看到初始化之后的默 ...