📌  相关文章
📜  国际空间研究组织 | ISRO CS 2018 |问题 36(1)

📅  最后修改于: 2023-12-03 15:23:04.504000             🧑  作者: Mango

ISRO CS 2018 - Problem 36

This problem is from the ISRO CS 2018 exam and it is Problem 36. The problem is about finding the largest string that can be formed by concatenating a given set of strings in any order except the given set of strings should not be repeated.

Problem Statement

Given a set of strings, your task is to find the largest string that can be formed by concatenating the given strings in any order except that the given strings should not be repeated.

Input

The input consists of two lines. The first line contains an integer N, which specifies the number of strings in the set. The second line contains N strings separated by a single space.

Output

The output consists of a single line containing the largest string that can be formed by concatenating the given strings in any order except that the given strings should not be repeated.

Example
Input
4
bat tab cat rat
Output
ratbatcat
Explanation

In this example, the given set of strings are "bat", "tab", "cat", and "rat". The largest string that can be formed by concatenating these strings in any order is "ratbatcat". The string "tab" is not repeated as per the problem statement.

Solution

To solve this problem, we can start by sorting the given set of strings in descending order of their length. We can then concatenate the strings one by one, checking if the resultant string already contains the previous string. If it already contains the previous string, we can skip that string and move on to the next string.

n = int(input())
words = input().split()

words.sort(key=lambda x: len(x), reverse=True)

result = ""

for word in words:
    if word not in result:
        result += word

print(result)

This approach has a time complexity of O(nlogn) due to the sorting of the strings.