NOIP2024,T1,python
def max_matching(T, data):
results = []
for _ in range(T):
n = int(data.pop(0))
s1 = data.pop(0)
s2 = data.pop(0)
t1 = data.pop(0)
t2 = data.pop(0)
fixed_matches = 0
swappable_segments = []
i = 0
while i < n:
if t1[i] == '1' and t2[i] == '1':
j = i
while j < n and (t1[j] == '1' and t2[j] == '1'):
j += 1
swappable_segments.append((s1[i:j], s2[i:j], t1[i:j], t2[i:j]))
i = j
else:
if s1[i] == s2[i]:
fixed_matches += 1
i += 1
max_swappable_matches = 0
for seg1, seg2, _, _ in swappable_segments:
count1 = [0, 0]
count2 = [0, 0]
for c in seg1:
count1[int(c)] += 1
for c in seg2:
count2[int(c)] += 1
min_count = min(count1[0], count2[0]) + min(count1[1], count2[1])
max_swappable_matches += min_count
total_matches = fixed_matches + max_swappable_matches
results.append(total_matches)
return results
import sys
input = sys.stdin.read
data = input().split()
T = int(data[0])
index = 1
results = max_matching(T, data[index:])
index += T * 5
for result in results:
print(result)
求调。
或者给一个C++代码,让我琢磨琢磨。