EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. Meta Coding Competitions
  2. 2011
  3. Qualification Round

Studious Student

https://www.facebook.com/codingcompetitions/hacker-cup/2011/qualification-round/problems/C

PreviousPeg GameNextRound 1A

Last updated 2 months ago

Question

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 Format

As input for playing this game you will receive a text file containing an integer N, the number of words 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.

Constraints

1≤N≤1001 \le N \le 1001≤N≤100
1≤M≤91 \le M \le 91≤M≤9
1≤all_word_lengths≤101 \le all\_word\_lengths \le 101≤all_word_lengths≤10

Output Format

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

Sample Inputs:

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

Output

cupfacebookforhackerstudentsstudious
duzklvrawqrc
dyroiymybeaxeyubxzdr
bwjibwjibwjijp
dcyihopjijliuiuy

58/63 Test Cases Solved

This is the one which I am working during the contest, and sadly it didn't pass all test cases. This code uses a lambda anonymous key function, which first pad all the text into the longest word, and sort by the alphabetical order.

def sort_words_by_length(sentence):
    words = sentence.split()[1:]
    max_len = max(len(word) for word in words)
    padded_words = [(word.ljust(max_len, 'z'), word) for word in words]
    sorted_words = sorted(padded_words)
    return ''.join(word[1] for word in sorted_words)

cases = int(input())
for i in range(1, cases+1):
	print("Case #%d: %s" % (i, sort_words_by_length(input())))

This may not work if the string has duplicated values.

Current Solution — Brute Force Permutation

The current working solution is simple — brute force all the permutation of the combinations and take the minimum as the result.

Personally, I am not a fan of using brute force to list out all possible combinations, but since it has only 63 test cases, I would take it.

Here's the solution, if you want simple and crude solution:

from itertools import permutations

def sort_words_by_length(sentence):
    words = sentence.split()[1:]
    return min("".join(p) for p in permutations(words))

cases = int(input())
for i in range(1, cases+1):
	print("Case #%d: %s" % (i, sort_words_by_length(input())))

Note that I imported a library called itertools, which it may or may not work in different contests. Luckily this contest accepts the library imports. Another note: this took O(N!) time complexity.