https://www.acmicpc.net/problem/5397

 

5397번: 키로거

첫째 줄에 테스트 케이스의 개수가 주어진다. 각 테스트 케이스는 한줄로 이루어져 있고, 강산이가 입력한 순서대로 길이가 L인 문자열이 주어진다. (1 ≤ L ≤ 1,000,000) 강산이가 백스페이스를 입

www.acmicpc.net

 

 

 

[난이도] Silver2
[유형] 리스트

[풀이]
STL list를 선언한 뒤 iterator를 커서로 취급하여 문제를 해결하면 됩니다.
insert나 erase시 return 되는 iterator의 위치는 헷갈릴 수 있기 때문에 몇가지 예시를 해보면서
iterator의 위치를 파악해야 합니다.

 

#include <iostream>
#include <list>
#include <string>
using namespace std;
int N;
string s;
int main(){
cin >> N;
while(N--){
cin >> s;
list<char> li;
auto it = li.begin();
for(auto c : s){
if(c=='-'){
if(it==li.begin()) continue;
it--;
it = li.erase(it);
}else if(c=='<'){
if(it==li.begin()) continue;
it--;
}else if(c=='>'){
if(it!=li.end()) it++;
}else{
li.insert(it,c);
}
}
for(auto v : li) cout << v;
cout << "\n";
}
}

 


https://github.com/has2/Problem-Solving/blob/master/boj-solved.ac/Silver2/5397.cpp

+ Recent posts