perl - Facebook黑客杯: Studious student problem

标签 perl

在资格赛期间,提出了以下问题:

You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the lexicographically lowest possible string.

Input

As input for playing this game you will receive a text file containing an integer N, the number of word sets you need to play your game against. This will be followed by N word sets, each starting with an integer M, the number of words in the set, followed by M words. All tokens in the input will be separated by some whitespace and, aside from N and M, will consist entirely of lowercase letters.

Output

Your submission should contain the lexicographically shortest strings for each corresponding word set, one per line and in order.

Constraints

1 <= N <= 100
1 <= M <= 9
1 <= all word lengths <= 10

Example input

5
6 facebook hacker cup for studious students
5 k duz q rc lvraw
5 mybea zdr yubx xe dyroiy
5 jibw ji jp bw jibw
5 uiuy hopji li j dcyi

Example output

cupfacebookforhackerstudentsstudious
duzklvrawqrc
dyroiymybeaxeyubxzdr
bwjibwjibwjijp
dcyihopjijliuiuy

我编写的程序如下:

chomp($numberElements=<STDIN>);  

for(my $i=0; $i < $numberElements; $i++)  
{  
   my $string;  
   chomp ($string = <STDIN>);  
   my @array=split(/\s+/,$string);  
   my $number=shift @array;  
   @sorted=sort @array;    
   $sortedStr=join("",@sorted);    
   push(@data,$sortedStr);  
}  

foreach (@data)  
{  
  print "$_\n";  
}  

该程序为给定的测试用例提供了正确的输出,但 facebook 仍然显示它是不正确的。程序有问题吗?

最佳答案

1
2 ba b

您的程序输出bba,这是不正确的。 bab 按字典顺序排列更早。

关于perl - Facebook黑客杯: Studious student problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4704184/

相关文章:

regex - 用于选择单间隔短语而不是空格的正则表达式

几次快速插入后,MySQL 插入变慢

javascript - 使用 CGI 方法通过 Web 服务器将参数从 JavaScript 传递到 Perl 脚本

linux - 为什么我的 perl 系统命令不起作用?

perl - 如何回读 Data::Dumper 的输出?

perl - 如何在 Perl 中将字符串转换为数字( float )

regex - 如何使用正则表达式捕获特定字符串?

perl - perl中如何找出两个日期之间的差异?

java - 读取文件并根据多个条件剪切每一行

perl - 如何获取正在运行的 Perl 脚本的路径和名称(C 中的 argv[0])