关于串序列长度压缩算法的构造

xt0899hw  于 2022-09-18  发布在  Java
关注(0)|答案(1)|浏览(169)

重复此操作,直到字符串%s为空。

如果从%s开头开始有两个或多个连续字母

・Define n as the number of consecutive pieces.
・Display 'ns' and delete n characters from the beginning.

如果两个或多个字符不连续

・Memorize how many characters are not consecutive and display '-ns'.
・Then remove ns from the string.

举例说明。

input = 'abcaaaaaaaaaaaaaab'
output = '-3abc12a-1b'

input = 'AaAaAAAAa'
output = '-4AaAa4A-1a'

如果您熟悉该算法,了解它将会很有帮助。

nlejzf6q

nlejzf6q1#

I would use a regular expression to identify the different chunks of the string, and then use the re.sub callback to format the chunk as specified:

import re

regex = re.compile(r"(.)\1+|(?:(.)(?!\2))+")

def encode_match(match):
    a, b = match.group(0, 1)
    return f"{'-' * (not b)}{len(a)}{b or a}"

def encode(s):
    return regex.sub(encode_match, s)

print(encode('abcaaaaaaaaaaaab')) # '-3abc12a-1b'
print(encode('AaAaAAAAa'))        # '-4AaAa4A-1a'

相关问题