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) ...
随机推荐
- linux 多线程之间信号传递
函数 sigwait sigwait的含义就如同它的字面意思:等待某个信号的到来.如果调用该函数的线程没有等到它想等待的信号那么该线程就休眠.要达到等到一个信号,我们得做下面的事: 首先,定义一个信号 ...
- Html5与本地App资料整理分析
最近开发了一个小的手机网站,重新了解了本地应用,html5,混合应用,webApp等概念,整理下相关的资料,略带自己的思考 用户固有的使用习惯 在<2012年度 HTML5状况及发展形势报告.p ...
- Jmeter 建立数据库测试计划
建立数据库测试计划(Building a Database Test Plan) 在本节中,将学习如何创建测试数据库服务器一个简单的测试计划.您将创建五十个用户向数据库服务器发送2个SQL请求.并且, ...
- Types的Type访问模式
在Types类中定义的访问都类如下: 1.MapVisitor类 2.SimpleVisitor 3.UnaryVisitor 4.TypeRelation
- Java中的语法树结构
1.JCTypeParameter class B<T extends CA&IA&IB> { ...} 截图如下: 接口继承了StatementTree接口,而实现类实现 ...
- 多线程编程(四)-CyclicBarrier的使用
CyclicBarrier的介绍 类CyclicBarrier不仅有CountDownLatch所具有的功能,还可以是啊县屏障等待的功能,也就是阶段性同步,它在使用上的意义在与可以循环地实现线程要一起 ...
- 学习Rails之activeAdmin
一.开始ActiveAdmin Active Admin是一个发布在RAILS3中使用的Gem. 1.我们为了快速开始我们对Active Admin的了解,我们首先安装它: 在你GemFile ...
- postgresql 备份与恢复
备份 C:\PostgreSQL\\bin>pg_dump -h localhost -p -U postgres Mes > d:/.bak 恢复 C:\PostgreSQL\\bin& ...
- mac terminal中快捷移动光标 持续更新。。。
1.option + ←/→ 以单词为单位快速移动 2.ctrl + A 移动到行首 3.ctrl + B 移动到行尾 4.ctrl + K 删除光标后至行尾的内容
- Html-完整表格
表头th 特殊的单元格:加粗.居中 它的用户是取代td的位置即可 <table border="2"> <tr> <th>姓名</th&g ...