Код: Выделить всё
def can_reduce_to_empty(s):
stack = []
while True:
n = len(s)
i = 0
new_s = []
while i < n:
char = s[i]
# Count consecutive occurrences
count = 1
while i + 1 < n and s[i + 1] == char:
count += 1
i += 1
# If the group has 2 or more consecutive occurrences, don't add it to new_s
print(f"left overs: {new_s}")
if count < 2:
new_s.append(char)
i += 1
# Join the new string after processing
s = ''.join(new_s)
# If the string has not changed, break out of the loop
if len(s) == n:
break
# If the string becomes empty, return True
return 1 if len(s) == 0 else 0
# Input and output handling
def main():
T = int(input().strip()) # Number of test cases
results = []
for _ in range(T):
s = input().strip() # Input string for each test case
results.append(can_reduce_to_empty(s))
print("\n".join(map(str, results)))
# Run the main function
if __name__ == "__main__":
main()
Я пробовал использовать матрицу что привело к той же проблеме, так что я предполагаю, что это логическая ошибка?
Подробнее здесь: https://stackoverflow.com/questions/792 ... ates-occur
Мобильная версия