알고리즘 분류
자료 구조
스택
연결 리스트
문제 해결 아이디어
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';
}
}
'Algorithm > 배열과 연결리스트' 카테고리의 다른 글
백준 에디터(1406) C++ 풀이 (0) | 2023.01.28 |
---|---|
백준 두 수의 합(3273) C++ 풀이 (0) | 2023.01.28 |
백준 방 번호(1475) C++ 풀이 (1) | 2023.01.28 |