Member 14877096 Ответов: 0

Может ли кто-нибудь предложить мне какой-нибудь другой метод? Какой-нибудь легкий способ?


Ques:1
Hitain is a very naughty boy in CSE Batch. One day he was playing with strings, and randomly shuffled them all. Your task is to help Hitain Sort all the strings (lexicographically) but if a string is present completely as a prefix in another string, then string with longer length should come first. Eg Cat, Catwoman are 2 strings and the string cat is present as a prefix in Catwoman - then sorted order should have - Catwoman, cat.
Constraints
N<1000
Output Format
N lines each containing one string.
Sample Input
3
cat
apple
catwoman
Sample Output

apple
catwoman
cat
bat
&l

Что я уже пробовал:

n = int(input())
arr=[]
for i in range(n):
    temp=input()
    arr.append(temp)
arr.sort(key=len, reverse=True)
for i in range(1, len(arr)-1):
    key1=arr[i]
    key = arr[i][:len(arr[i+1])]
    j = i-1
    while j >=0 and key < arr[j] : 
        arr[j+1] = arr[j] 
        j -= 1
    arr[j+1] = key1 
for i in range(len(arr)):
    print(arr[i])

Richard MacCutchan

Я не знаю, откуда взялась летучая мышь. Может, тебе стоит закрыть окно?

0 Ответов