![](https://i-blog.csdnimg.cn/blog_migrate/f98b3159dd521790ab93a822a03c1ca2.png)
代码:
import sys
str1 = input()
str2 = []
temp = ''
for s in str1:
if s == temp:
str2[-1][1] += 1
continue
str2.append([s, 1, 0])
temp = s
flag = False
length = len(str2)
for j in range(2 ** 64):
if length == 0:
print("EMPTY")
sys.exit(0)
if length == 1:
print(str2[0][0]*str2[0][1])
sys.exit(0)
if flag:
str3 = [s[0]*s[1] for s in str2]
print(''.join(str3))
sys.exit(0)
for i in range(1, len(str2)):
if str2[i][1] > 1:
str2[i - 1][2] -= 1
str2[i][2] -= 1
elif str2[i - 1][1] > 1:
str2[i - 1][2] -= 1
str2[i][2] -= 1
flag = True
i = 0
while i < len(str2):
s = str2[i]
if s[2] < 0:
flag = False
s[1] += s[2]
s[2] = 0
if s[1] <= 0:
del str2[i]
continue
i += 1
i = 1
while i < len(str2):
if str2[i][0] == str2[i - 1][0]:
str2[i - 1][1] += str2[i][1]
del str2[i]
continue
i += 1
length = len(str2)
# str3 = [s[0] * s[1] for s in str2]
# print(''.join(str3))
str3 = [s[0]*s[1] for s in str2]
print(''.join(str3))