原题地址链接:
问题描述:
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 4hiyesfoo barhello worldOutput Case #1: 44 444Case #2: 999337777Case #3: 333666 6660 022 2777Case #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:$!\n"; 7 open $out,'>',$outfile 8 or die "Cannot open $outfile:$!\n"; 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{$_}'\n"; 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' =======================:\n" if $debug; 38 print "length($line)=",length($line),"\n" if $debug; 39 for(my $index=0;$index
上传原题地址链接网站,结果正确。