72 lines
1.3 KiB
C++
72 lines
1.3 KiB
C++
#include <iostream>
|
|
#include <cctype>
|
|
#include <cstdlib>
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
std::string decode(std::string& strIn, std::string::iterator& itr, bool firstCircle = false)
|
|
{
|
|
std::string out;
|
|
|
|
while (itr != strIn.end())
|
|
{
|
|
std::string strNum;
|
|
auto repeat = 1;
|
|
|
|
if (std::isdigit(static_cast<unsigned>(*itr)))
|
|
{
|
|
while (*itr != '[')
|
|
{
|
|
strNum += *itr++;
|
|
}
|
|
repeat = std::atoi(strNum.c_str());
|
|
auto rptOut = decode(strIn, ++itr);
|
|
|
|
for (int i = 0; i < repeat; ++i)
|
|
out += rptOut;
|
|
}
|
|
|
|
if (*itr == ']')
|
|
{
|
|
if (!firstCircle)
|
|
return out;
|
|
}
|
|
else
|
|
out += *itr;
|
|
|
|
++itr;
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
std::string kek(std::string strIn)
|
|
{
|
|
std::cout << strIn << std::endl;
|
|
auto itr = strIn.begin();
|
|
|
|
return decode(strIn, itr, true);
|
|
}
|
|
|
|
int main()
|
|
{
|
|
std::string strIn;
|
|
|
|
strIn = "3[a2[c]]";
|
|
std::cout << kek(strIn) << std::endl;
|
|
|
|
strIn = "c2[a]3[b]c";
|
|
std::cout << kek(strIn) << std::endl;
|
|
|
|
strIn = "c2[a3[b]]c";
|
|
std::cout << kek(strIn) << std::endl;
|
|
|
|
strIn = "231[ab]";
|
|
std::cout << kek(strIn) << std::endl;
|
|
|
|
strIn = "3[b15[a]]";
|
|
std::cout << kek(strIn) << std::endl;
|
|
|
|
return 0;
|
|
}
|