在资格赛期间,提出了以下问题:
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/