返回题解分享
讨论 / 题解分享/ 帖子详情

外星密码 - 题解

def decode_string(s):
    def decode_helper(index):
        decoded_string = ''
        num = 0

        while index < len(s):
            char = s[index]

            if char.isdigit():
                num = num * 10 + int(char)
            elif char == '[':
                index, decoded_part = decode_helper(index + 1)
                decoded_string += decoded_part
                # num = 0
            elif char == ']':
                if num:
                    decoded_string = num * decoded_string
                return index, decoded_string
            else:
                decoded_string += char

            index += 1

        return decoded_string

    return decode_helper(0)


# 输入的压缩字符串
compressed_string = input()

# 解压缩后的字符串
decoded_string = decode_string(compressed_string)
print(decoded_string)
0 回复 0 转发 0 喜欢 5 阅读
回复 (0)
默认 最新
暂无回复,快来抢沙发!