一、题目
有一种简易压缩算法:针对由全部小写字母组成的字符串,将其中连续超过两个相同字目的部分压缩成连续个数加该字母,其他部分保持原样不变。
例如,字符串:aaabccccd 经过压缩成为字符串:3ab4cd。请您编写一个unZip函数,根据输入的字符串,判断其是否为合法压缩过的字符串。
若输入合法,则输出解压后的字符串,否则输出:!error 来报告错误。
测试:3ab4cd合法,aa4b合法,caa4b合法,3aa4b不合法,22aa不合法,2a4b不合法,22a合法,3a3a不合法
二、代码
// 找到某个位置前的字符
void baseHuawei::OnFindChar(std::string str, int pos, char & beforeChar, char & afteChar)
{
if (str.empty())
return;
for (int i = pos; i < str.length(); i++)
{
if (str[i]-'0'>=0&&str[i]-'0'<=9)
continue;
afteChar = str[i];
break;
}
for (int i = pos; i >=0; i--)
&n