蓝桥杯3520 翻转 | 贪心+分类讨论
题目传送门
这个题目是一道贪心题(局部最优次数可以推出整体最优次数),可以按照顺序遍历S数组,发现满足翻转条件的下标进行翻转,最后判断是否与T相等即可。
d = int(input())
while d > 0:
t = list(input())
s = list(input())
cnt = 0
for i in range(1, len(s)-1):
if s[i] != t[i]:
if s[i-1] == t[i-1] and s[i+1] == t[i+1] and s[i-1] == s[i+1] and s[i-1] != s[i]:
s[i] = s[i-1]
cnt += 1
else:
break
if s == t:
print(cnt)
else:
print(-1)
d -= 1
END✨