def multi_comp(s):     le = len(s)     if le==0:         return ''     if len(set(s))==1:         if le>1:             return '{}{}'.format(s[0], le)         return s     for i in range(le//2, 0, -1):         for j in range(le-i):             res = []             ind = j             while ind+2*i<=le:                 if s[ind:ind+i]==s[ind+i:ind+2*i]:                     res.append(ind)                     ind += i                 else:                     break             if len(res)>0:                 if len(set(s[res[0]:res[0]+i]))==1:                     ret = '{}{}'.format(s[res[0]],res[-1]+2*i-res[0])                 else:                     ret = '({}){}'.format(s[res[0]:res[0]+i], len(res)+1)                 left = multi_comp(s[:res[0]])                 right = multi_comp(s[res[-1]+2*i:])                 return left+ret+right     return s 多字符串压缩大概写了一下,没怎么测过,不知道对不对。
点赞 评论

相关推荐

赏个offer求你了:友塔HR还专门加我告诉我初筛不通过😂
点赞 评论 收藏
分享
牛客网
牛客企业服务