114 lines
2.6 KiB
C++
114 lines
2.6 KiB
C++
|
|
#include <iostream>
|
|||
|
|
|
|||
|
|
using namespace std;
|
|||
|
|
|
|||
|
|
/******************************************************************************
|
|||
|
|
|
|||
|
|
Online C++ Compiler.
|
|||
|
|
Code, Compile, Run and Debug C++ program online.
|
|||
|
|
Write your code in this editor and press "Run" button to compile and execute it.
|
|||
|
|
|
|||
|
|
https://www.onlinegdb.com/online_c++_compiler
|
|||
|
|
|
|||
|
|
*******************************************************************************/
|
|||
|
|
|
|||
|
|
#include <cctype>
|
|||
|
|
#include <cstdlib>
|
|||
|
|
#include <iostream>
|
|||
|
|
#include <string>
|
|||
|
|
|
|||
|
|
// std::string kek(std::string strIn, int pos = 0)
|
|||
|
|
// {
|
|||
|
|
// std::cout << strIn << std::endl;
|
|||
|
|
|
|||
|
|
// for (int i = pos; i < strIn.size(); i++)
|
|||
|
|
// {
|
|||
|
|
// std::string strNum;
|
|||
|
|
// if (std::isdigit(static_cast<unsigned>(strIn[i])))
|
|||
|
|
// { // Если число
|
|||
|
|
// while (true)
|
|||
|
|
// {
|
|||
|
|
// strNum += strIn[i++];
|
|||
|
|
// if (strIn[i] == '[')
|
|||
|
|
// break; // Буквы или цифры начнутся со следующей буквы
|
|||
|
|
// }
|
|||
|
|
// auto repeat = std::atoi(strNum.c_str());
|
|||
|
|
// std::cout << repeat << std::endl;
|
|||
|
|
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
// return "return";
|
|||
|
|
// }
|
|||
|
|
|
|||
|
|
std::string decode(std::string& strIn, std::string::iterator& itr)
|
|||
|
|
{
|
|||
|
|
std::string out;
|
|||
|
|
|
|||
|
|
while (itr != strIn.end())
|
|||
|
|
{
|
|||
|
|
std::string strNum;
|
|||
|
|
auto repeat = 1;
|
|||
|
|
|
|||
|
|
auto itr_char = *itr; // DELME
|
|||
|
|
|
|||
|
|
if (std::isdigit(static_cast<unsigned>(*itr)))
|
|||
|
|
{
|
|||
|
|
while (*itr != '[')
|
|||
|
|
{
|
|||
|
|
strNum += *itr++;
|
|||
|
|
}
|
|||
|
|
repeat = std::atoi(strNum.c_str());
|
|||
|
|
// std::cout << repeat << std::endl; // DELME
|
|||
|
|
auto tmp = decode(strIn, ++itr);
|
|||
|
|
|
|||
|
|
for (int i = 0; i < repeat; ++i)
|
|||
|
|
out += tmp;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (*itr == ']')
|
|||
|
|
{
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
|
|||
|
|
// std::string wait;
|
|||
|
|
// std::cin >> wait;
|
|||
|
|
return 0;
|
|||
|
|
}
|