テンセント2020校募集筆記試験:圧縮文字


タイトルの説明
形如:AB[2|OP]R[2|Y[3|S]]の圧縮文字は、ABOPOPRYSSSYSSS
構想
再帰解凍
C++コード
string unzip(string s)
{
	string res;
	int i = 0;
	int n = s.size();
	while (i < n)
	{
		while (i < n&&s[i] != '[')
		{
			res.push_back(s[i]);
			i++;
		}
		if (i == n)
			break;
		i++;
		int bety = 0,start=i;
		while (i < n&&s[i] >= '0'&&s[i] <= '9')
		{
			bety++;
			i++;
		}
		string num = s.substr(start, bety);
		int value = stoi(num);
		if (s[i] == '|')
		{
			i++;
			int begin = i,left=1,right=0;
			while (iright)
			{
				if (s[i] == '[')
					left++;
				if (s[i] == ']')
					right++;
				i++;
			}
			int len = i - begin - 1;
			string subt=unzip(s.substr(begin, len));
			for (int i = 0; i < value; i++)
				res = res + subt;
		}

	}
	return res;

}