其他分享
首页 > 其他分享> > 洛谷P1928 外星密码进阶解法

洛谷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;
}

标签:sub,cypher,洛谷,进阶,int,P1928,substr,right,left
来源: https://blog.csdn.net/weixin_52075219/article/details/117933034