publicstaticvoidmain(String[] args) { Scannerscanner=newScanner(System.in); // The furthest distance in the world,Is not between life and death,But when I stand in front or you,Yet you don't know that I love you. Stringwords= scanner.nextLine(); // 待搜素的单词:f Stringword= scanner.nextLine(); List<String> ans = search(words, word);
// front furthest System.out.println(String.join(" ", ans)); }
# 从words字符串分词并查找以word开头的单词列表 defsearch(words, word): # 使用正则表达式分割单词,\w匹配字母、数字或下划线 word_set = {w for w in re.split(r'\W+', words) if w}
# 使用列表推导式查找以word开头的单词 res = [w for w in word_set if w.startswith(word)]
# 如果没有找到任何匹配项,添加word ifnot res: res.append(word)
return res
if __name__ == '__main__': # The furthest distance in the world,Is not between life and death,But when I stand in front or you,Yet you don't know that I love you. words = input() # f word = input() ans = search(words, word) # front furthest print(' '.join(sorted(ans)))