题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
words = input().strip().split() lengths = [] alphas = [] others = [] for word in words: lengths.append(len(word)) for w in word: if not w.isalpha(): others.append((len(alphas) + len(others), w)) else: alphas.append(w) def insert_sort(val: list[str]): for i in range(len(val)): if not val[i].isalpha(): continue j = i - 1 cur = val[i] while j >= 0: if not val[j].isalpha(): j -= 1 continue if cur == val[j] or cur.lower() == val[j].lower(): if not val[j + 1].isalpha(): val[j + 2] = cur else: val[j + 1] = cur break elif cur.lower() < val[j].lower(): if not val[j + 1].isalpha(): val[j + 2] = val[j] else: val[j + 1] = val[j] j -= 1 elif cur.lower() > val[j].lower(): val[j + 1] = cur break if cur.lower() < val[0].lower(): val[0] = cur insert_sort(alphas) for other in others: alphas.insert(other[0], other[1]) cur = 0 for l in lengths: print("".join(alphas[cur : l + cur]), end=" ") cur += l