1、題目描述
m個連續相同的字符串S將會壓縮爲[m|S],字符串ABCABCABC會被壓縮爲[3|ABC]
請把壓縮後的字符串解壓。
示例:
輸入
HG[3|B[2|CA]]F
輸出:
HGBCACABCACABCACAF
代碼C++:
#include <iostream>
#include <string>
using namespace std;
static int index;
string rezip(string str, int count) {
string sb = "";
int num = 0;
bool c = true;
for (; index < str.length(); index++) {
if (str[index] == '[') {
c = false;
continue;
}
if (str[index] == '|') {
index++;
string reStr = rezip(str, num);//讀到 ] 時函數返回
sb += reStr;
c = true;
continue;
}
if (str[index] == ']')
break;
if (c == true)
sb += str[index];
if (c == false)//數字
num = num * 10 + (str[index] - '0');
}
string ressb = "";
for (int i = 0; i < count; i++)
ressb += sb;
return ressb;
}
int main() {
string str;
getline(cin, str);
index = 0;
cout << rezip(str, 1) << endl;
system("pause");
return 0;
}