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) ...
随机推荐
- OpenFoam+CFDEM+Liggghts安装耦合
这里安装的时间节点为:2018.10.29,安装的是目前的最新版本CFDEM,支持到与OpenFoam-5.x的耦合. 1. 先安装openfoam:https://openfoam.org/down ...
- Java中HashMap的hash分布策略的简单解释
趴源码是看到一段不可思议的代码,网上的解释似乎不大令人满意,因此稍微花点时间解读了一下,如有错误请指正 HashMap的桶是这样搞的 // 片段1 static final int hash(Obje ...
- 【性能测试】脚本开发,最普通的http协议脚本2
Action() { lr_start_transaction("FM0075基金购买"); web_submit_data("ehouse_ehGetPwdRandom ...
- 用Jquery获取Url的参数
在网上找的一个办法 //先写一个方法 function GetUrlString(name) { var reg = new RegExp("(^|&)"+ name +& ...
- 不支持这个操作系统WNT_6.3I_64
安装winserver2012驱动时,经常会因为版本的关系,出现向后兼容问题: 编辑驱动安装配置ini, 添加向后兼容的标识即可:WNT_6.3I_64= Win81_64 删除system下的程序( ...
- Linux/Mac安装oh-my-zsh后不执行~/.bash_profile、~/.bashrc解决办法
安装了zsh之后默认启动执行脚本变为了-/.zshrc. 解决办法: Mac: 修改-/.zshrc文件,在其中添加:source -/.bash_profile.source -/.bashrc:注 ...
- Jmeter创建一个点对点的 JMS 测试计划
创建一个点对点的 JMS 测试计划 确保所需的jar文件位于JMeter lib目录中. 如果没有,关闭JMeter,复制jar文件并重新启动JMeter. 参见详细教程 在本节中,将学习如何创建测试 ...
- python-cgi-demo
简单的Python CGI 在linux平台实现注意:路径是以当前路径为根目录 ,Python文件一般放在/cgi-bin/目录下在linux命令行运行:python -m CGIHTTPServ ...
- 低版本的linux系统装samba服务器
这里所用系统fedora14,安装samba服务器.我是壮壮熊. 由于工作原因,需要在feaord14上装samba服务器. 问题描述:用yum -y install samba安装samba后,需要 ...
- 724_Find-Pivot-Index
目录 724_Find-Pivot-Index Description Solution Java solution Python solution 724_Find-Pivot-Index Desc ...