实现简易字符串压缩算法:一个长度最大为128的字符串,
由字母a-z或者A-Z组成,将其中连续出现2次以上(含2次)的字母转换为字母和出现次数,以达到压缩目的。
样例输入:
AAAABBBB
样例输出:
A4B4
代码如下:
str1 = "";
str1 = io.read();
strend = "";
sum = 1;
i = 1;
j = 1;
while i <= string.len(str1) and j <= string.len(str1) do
j = i+1;
if(string.sub(str1,j,j) == string.sub(str1,i,i)) then
sum = sum + 1;
else
strend = strend..string.sub(str1,i,i)..sum ;
sum = 1;
end
i = i + 1;
end
print(strend)
用惯了C++,格式真是不习惯