问题描述
我很难在符号序列上使用RLE.
I have difficulties with how to use RLE on sequences of symbols.
例如,我可以对像这样的字符串进行RLE编码
For example, I can do RLE encoding on strings like
"ASSSAAAEERRRRRRRR"
它将被转换为:
"A1S3A3E2R8".
但是我想对像这样的字符串执行RLE
But I'd like to perform RLE on strings like
"XXXYYYYY(1ADEFC)(EDCADD)(1ADEFC)(1ADEFC)(1ADEFC)"
它将被转换为:
"X3Y5(1ADEFC)1(EDCADD)1(1ADEFC)3"
有没有办法做到这一点?因为长字符串总是放在方括号中,所以这项工作变得容易一些.可以提供建议使用C ++进行此操作吗?
如果有比使用方括号更好的方法来存储值,那么如果您推荐我,那也将很棒.
Is there is a way to reach it? This job becomes a bit easer because long strings always follows in brackets. Could give an advice to do this in C++?
If there is a better way to store values than using brackets, it will be also great if you recommend me.
推荐答案
您应该将此问题分解为更小的部分.首先,您应该具有一个标记您的流并返回每个单独部分的函数.对于此示例输入流:
You should break down this problem into smaller parts. First, you should have a function that tokenizes your stream and returns each individual part. For this example input stream:
"XXXYYYYY(1ADEFC)(EDCADD)(1ADEFC)(1ADEFC)(1ADEFC)"
此函数将返回以下元素,每个调用返回一个元素:
this function will return the following elements, one per call:
X
X
X
Y
Y
Y
Y
Y
(1ADEFC)
(EDCADD)
(1ADEFC)
(1ADEFC)
(1ADEFC)
<eof>
如果正确实现了此功能,则应该轻松地为单个字符实现已经实现的RLE算法,以支持更长的字符串.
If you get this function correctly implemented, then the RLE algorithm that you already implemented for single characters should be easily adapted to support longer strings.
这篇关于C ++和RLE用于符号序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!