[蓝桥杯 2025 省 Python B] 最多次数
import sysdef max_times() -> int:s = sys.stdin.readline().strip()checked = {'l','q','b'} # set(),不存在键值对,识别为set()n = len(s)time = 0i = 0while i < n - 2:sec = s[i:i+3]if set(sec) == checked:i += 3time += 1else:i += 1sys.stdout.write(str(time))if __name__ == "__main__":max_times()
ps:
解法关键:处理片段切割问题,片段切割后,里面的任何部分都不可再使用
-----------------------------------------------------------------------------------------------------------------------------
变体:
处理片段切割问题,片段切割后,里面的任何部分可再使用
import sysdef max_count() -> int:s = sys.stdin.readline().strip()checked = {'l','q','b'} # set(),不存在键值对,识别为set()n = len(s)set_ = set() # 存贮没有重复的组合 # {} --> 空字典 set --> 空集合for i in range(n - 2):sec = s[i:i+3]if set(sec) == checked and sec not in set_:set_.add(sec)else:continuesys.stdout.write(str(len(set_)))if __name__ == "__main__":max_count()