[ 문제 ] : https://www.acmicpc.net/problem/5397
5397번: 키로거
첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한줄로 이루어져 있고, 강산이가 입력한 순서대로 길이가 L인 문자열이 주어진다. (1 ≤ L ≤ 1,000,000) 강산이가 백스페이스를 입
www.acmicpc.net
[ 문제 접근 ]
- BOJ 1406 [에디터] 문제랑 똑같은 문제이다.
기본적인 List의 iterator를 cursor라고 생각하고 조작하는 문제.
[ 최종 코드 ]
#include<iostream> #include<algorithm> #include<list> #include<string> using std::cout; using std::cin; using std::string; using std::list; int t; int main() { std::ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cin >> t; string password; list<char> str = {}; while (t--) { cin >> password; list<char>::iterator cursor = str.end(); for (auto elem : password) { if (elem == '<') { if (str.size() && cursor != str.begin()) { cursor--; } } else if (elem == '>') { if (str.size() && cursor != str.end()) { cursor++; } } else if (elem == '-') { if (cursor != str.begin()) { cursor = str.erase(--cursor); } } else { str.insert(cursor,elem); } } for (auto elem : str) { cout << elem; } cout << '\n'; str.clear(); } return 0; }
[ Key Point ]
👉 처음에 list<char> str에 든게 없는데 cursor를 어디다 설정해야할지 고민했었다.
list<char>::iterator cursor = str.end();
비어 있는 list의 begin()이나 end()를 사용해도 된다는 점을 새로 알았다.
이때 begin() 이나 end() 나 같은 값이므로 아무거나로 초기화 해주면 된다.
[ 다른 사람 풀이 ]
ref :: https://congcoding.tistory.com/48
'PS > BaekJoon' 카테고리의 다른 글
[1874] 스택 수열 - 스택 ★★☆☆ / 복습 ○ (0) | 2021.11.24 |
---|---|
[1158] 요세푸스 - 큐, 연결 리스트 ★★☆☆ / 복습 ○ (0) | 2021.11.24 |
[1406] 에디터 - 연결 리스트 ★☆☆☆ / 복습 ● (0) | 2021.11.23 |
[3273] 두 수의 합 - 배열/투 포인터 ★☆☆☆ / 복습 ○ (0) | 2021.11.23 |
[1475] 방 번호 - 배열 ★☆☆☆ / 복습 ● (0) | 2021.11.23 |