string='AHYAHYAHAHAHAHAHAHAHBBBBBBBTATAZAB*+I'
length=len(string)
half=round(length/2)
new_string=""
i=1
while i<=half and string:
if i>length-i:
pass
sub_string1=string[:i]
sub_string2=string[i:i+i]
if sub_string1==sub_string2:
match=True
count=1
while match is True:
sub_string1=string[count*i:(count+1)*i]
sub_string2=string[(count+1)*i:(count+2)*i]
if sub_string1 == sub_string2:
count+=1
else:
match=False
new_string+="("+str(count+1)+")"+sub_string1
string=string[count*i+i:]
i=1
else:
if i==len(string):
new_string+=string[0]
string=string[1:]
i=1
else:
i+=1
print(new_string)
(2)AHY(7)AH(7)B(2)TAZAB*+I
以下是我为解决我的问题所做的。 我还没有彻底检查edge案例,但它正在对我的测试输入进行处理。 也许这对将来的人会有帮助。 它是游程编码,但用于字符组,而不是单个字符。从我所读到的,正常的RLE将AAAAHA编码为A4H1A1H1A1,而我需要编码4A2HA
相关问题 更多 >
编程相关推荐