原题地址链接: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 4 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算法:
1 #!/usr/bin/perl 2 my $debug=0; #该问题略微复杂,所以使用调试技术:如果$debug为1,则结果显示在标准输出上,而不输出到 .out 文件内;如果$debug 为0,则结果直接输出到 .out 文件中。 3 my $infile='C-large-practice.in'; 4 my $outfile='C-large.out'; 5 open $in,'<',$infile 6 or die "Cannot open $infile:$! "; 7 open $out,'>',$outfile 8 or die "Cannot open $outfile:$! "; 9 #建立哈希表 10 my %dict=( 11 2=>"abc", 12 3=>"def", 13 4=>"ghi", 14 5=>"jkl", 15 6=>"mno", 16 7=>"pqrs", 17 8=>"tuv", 18 9=>"wxyz", 19 0=>" " 20 ); 21 22 if($debug){ 23 for(keys %dict){ 24 print "$_=>'$dict{$_}' "; 25 } 26 } 27 chomp(my $N=<$in>); 28 my $str; 29 my $line; 30 my $prev,$cur; 31 $N=5 if $debug; 32 for($i=1;$i<=$N;$i++){ 33 $str=""; 34 $prev=""; 35 chomp($line=<$in>); 36 my $temp=""; 37 print "===================== For $line='$line' =======================: " if $debug; 38 print "length($line)=",length($line)," " if $debug; 39 for(my $index=0;$index<length($line);$index++){ 40 $cur=substr($line,$index,1); 41 $temp .=$cur if $debug; 42 print "$prev='$prev',$cur='$cur',$index='$index',$temp='$temp' " if $debug; 43 #if($cur eq $prev){ 44 # print "'$cur' eqs '$prev' " if $debug; 45 # $str .=" "; 46 #}#是否需要pause的关键不是 当前字符 $cur 与上一个字符 $prev 是否相等,而是当前需要按下的按键 $key 是否与上一个按键 $prev 是否相等,因此注释掉第一次使用的算法 47 LOOP1: while(($key,$value)=each %dict){ 48 if((my $pos=index($value,"$cur"))!=-1){ 49 print "find '$cur' at $pos in '$value' for '$key' " if $debug; 50 if( $key eq $prev){ #是否需要pause的关键不是当前字符是否和上一个字符相等,而是当前需要按下的按键和上一个按键是否相等 51 print "'$key' eq '$prev' " if $debug; 52 $str .=" "; 53 } 54 $prev=$key;#将当前按键存储到上一个按键中,以便下一次比较 55 $str .= $key x ($pos+1); 56 # last LOOP1; #即使已经找到了匹配的键值,也不能提前跳出循环,http://www.cnblogs.com/dongling/p/5705224.html 这篇随笔解释了原因 57 } 58 } 59 } 60 if($debug){ 61 print "Case #$i: $str "; 62 } 63 else{ 64 print $out "Case #$i: $str "; 65 } 66 }
上传原题地址链接网站,结果正确。