描述

1)如果s包含长度超过1的由相同字母组成的子串，那么这些子串会被同时消除，余下的子串拼成新的字符串。例如"ABCCBCCCAA"中"CC",“CCC"和"AA"会被同时消除，余下"AB"和"B"拼成新的字符串"ABB”。

2)上述消除会反复一轮一轮进行，直到新的字符串不包含相邻的相同字符为止。例如”ABCCBCCCAA”经过一轮消除得到"ABB"，再经过一轮消除得到"A"

3
ABCBCCCAA
AAA
ABC

9
4
2

AC的python代码

listt = list()
num = int(raw_input())
for i in range(num):
listt.append(raw_input())

for i in range(num):  #process the string one by one
lenn = len(listt[i])
m = list(listt[i])
res = 0
if lenn == 0:
print(0)
continue
elif lenn == 1:
print(2)
continue
for cc in range(3 * (lenn + 1)):
m = list(listt[i])
if cc % 3 == 0:
m.insert(cc // 3, "A")
elif cc % 3 == 1:
m.insert(cc // 3, "B")
else:
m.insert(cc // 3, "C")
#print(m, end = "   ")
flag = 1     # used to show whether the string has been changed
while(flag):
leng = len(m)
if leng <= 1:
break
k = -1
for j in range(leng - 1):   # to find the index which m[i] == m[k - 1]
if j < k:
m[j] = "D"
if m[j] == m[j + 1]:
k = j + 2
while((k < leng) and (m[j] == m[k])):
k = k + 1
if k == leng:             # deal with the special condition where k is the end
m[k - 1] ="D"
m[j] = "D"     # make the continuous alphabet "D"
if "D" not in m:
flag = 0
while("D" in m):
m.remove("D")     #remove all the "D" from the string
leng = len(m)
#print(leng)
res = max(res, lenn +1 - leng)
print(res)


biu…

1. 这个操作应该是遍历吧，把所有的可能性都筛选一遍。
2. 审题真的很重要，我原来以为是需要先进行删除再添加字母，后来发现是先添加字母再删除，之间的差距还是蛮大的。
3. 路漫漫其修远兮。

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试