본문 바로가기

Algorithm/배열과 연결리스트

백준 키로거(5397) C++ 풀이

알고리즘 분류

자료 구조

스택

연결 리스트

문제 해결 아이디어

list와 iterator를 이용하여 자신이 지금 어디를 순회하고 있고, 어디로 이동하려 하는지를 알면 해결 할 수 있는 문제이다.

코드

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

int t;
string s;
int main()
{
	cin >> t;

	while (t--)
	{
		list<char> l;
		list<char>::iterator it = l.end();

		cin >> s;

		for (char str : s)
		{
			if (str == '<')
			{
				if (it != l.begin()) it--;
			}
			else if (str == '>')
			{
				if (it != l.end()) it++;
			}
			else if (str == '-')
			{
				if (it != l.begin()) l.erase(prev(it));
			}
			else
				l.insert(it, str);
		}

		for (char str : l)
		{
			cout << str;
		}
		cout << '\n';
	}
}