"""
题目来源:
https://www.lanqiao.cn/problems/3520/learning/?page=1&first_category_id=1&name=%E7%BF%BB%E8%BD%AC
"""
import os
import sys
import copy
# 请在此输入您的代码
n = int(input())
# n组测评数据
data = [[] for i in range(n)]
for i in range(2 * n):
s = list(input())
data[i // 2].append(s)
# 对s和t翻转需要的次数
def check(s, t):
# 如果S为''或者'0'或者'1'不需要翻转
length = len(s)
if length <= 3:
return 0
# 记录翻转次数
ans = 0
# 记录数据s和t的变化
lis = [[s, t]]
# 深复制, 避免在对s和t修改的过程中覆盖掉之前的记录
s1, t1 = copy.deepcopy(s), copy.deepcopy(t)
while s1 != t1:
for i in range(1, len(s1) - 1):
if s1[i] != t1[i]:
# 可以对s进行翻转, 也可以对t进行翻转, 只要其中一个串满足翻转条件就可以
if s1[i] != s1[i - 1] and s1[i - 1] == s1[i + 1]:
s1[i] = t1[i]
if t1[i] != t1[i - 1] and t1[i - 1] == t1[i + 1]:
t1[i] = s1[i]
ans += 1
# 记录经过翻转后的两个串s和t
lis.append([s1, t1])
# 如果发现经过一轮翻转后两个串并没有发生任何变化, 说明无法通过翻转将s和t变一样
if lis[-1] == lis[-2]:
return -1
s1, t1 = copy.deepcopy(s1), copy.deepcopy(t1)
return ans
for S, T in data:
# 在串的前后加个'*', 是为了方便可以对串的所有字符都进行一次翻转, 不影响结果
print(check(['*'] + S + ['*'], ['*'] + T + ['*']))
标签:range,蓝桥,length,input,import,data,翻转
From: https://blog.csdn.net/weixin_60193316/article/details/136895724