📅  最后修改于: 2023-12-03 15:23:04.504000             🧑  作者: Mango
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.
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.
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.
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.
4
bat tab cat rat
ratbatcat
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.
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.