def longest_vowel_substring(flaw, s):vowels = set("aeiouAEIOU")max_length = 0n = len(s)# 预处理,记录所有元音字母的位置vowel_indices = [i for i, char in enumerate(s) if char in vowels]# 如果没有元音字母,直接返回0if not vowel_indices:return 0# 使用滑动窗口来找到最长的符合条件的子串for i in range(len(vowel_indices)):for j in range(i + 1, len(vowel_indices)):start = vowel_indices[i]end = vowel_indices[j]# 计算当前子串中的非元音字母数量flaw_count = 0for k in range(start + 1, end):if s[k] not in vowels:flaw_count += 1# 如果瑕疵度匹配,更新最大长度if flaw_count == flaw:max_length = max(max_length, end - start + 1)# 如果瑕疵度已经超过预期,跳出内层循环if flaw_count > flaw:breakreturn max_length# 读取输入flaw = int(input)s = input.strip# 计算并输出结果print(longest_vowel_substring(flaw, s))摘要:def longest_vowel_substring(flaw, s):vowels = set("aeiouAEIOU")max_length = 0n = len(s)# 预处理,记录所有元音字母的位置vowel_indices = [i for i,
来源:振家教育