2022年7月18日 星期一

i524: 11988: Broken Keyboard (a.k.a. Beiju Text)

程式碼

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;
	while (cin >> s)
	{
		string ans = "";
		int index = 0;
		for (int i = 0; i < s.size(); i++)
		{
			if (s[i] == '[')
			{
				index = 0;
			}
			else if (s[i] == ']')
			{
				index = ans.size() ;
				if (index < 0) index = 0;
			}
			else
			{
				ans.insert(index, string(1, s[i]));
				index++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}