洛谷P1928 外星密码进阶解法

#include<iostream>
#include<string>

using namespace std;
string cypher;

void decypher()
{

	for (;;)
	{
		int right = cypher.find_first_of(']');
		if (-1 == right)
			return;
		int left = cypher.substr(0, right).find_last_of('[');
		string sub = "", temp = "";
		int length = 0;
		if (isdigit(cypher[left + 2]))
		{
			length = (cypher[left + 1] - '0') * 10 + cypher[left + 2] - '0';
			sub = cypher.substr(left + 3, right - left - 3);
		}
		else
		{
			length = cypher[left + 1] - '0';
			sub = cypher.substr(left + 2, right - left - 2);
		}
		for (int i = 0; i < length; i++)
			temp += sub;
		cypher = cypher.substr(0, left) + temp + cypher.substr(right + 1);
	}
}

int main()
{
	cin >> cypher;
	decypher();
	cout << cypher;
	return 0;
}
上一篇:LeetCode之无重复字符串的最大子串(三)


下一篇:JavaScript对象——每天一点丝滑