-
5397) 키로거 c++백준코딩일기 2020. 11. 5. 13:46
문제 ) www.acmicpc.net/problem/5397
풀이 )
ltr = ans.end() 로 지정하여 ans의 오른쪽을 가리키게 설정한다.
입력받은 문자열만큼 반복해서 확인을 하는데,
1.'<' 문자가 들어왔을 경우 커서를 왼쪽으로 움직여야하므로
itr = ans.begin() 이라면 커서는 움직임 없이 그대로 있지만, 아니라면 itr--2. '>' 문자가 들어왔을 경우 커서를 오른쪽으로 움직여야하므로
itr = ans.end() 라면 커서는 글자 뒤 즉, 오른편에 있지만, 아니라면 itr++3. '-' 문자가 들어왔을 경우 현재 커서위치 앞에 글자가 존재하면 그 글자를 삭제한다.
itr = ans.begin() 맨 앞이라면 그냥 넘어가지만 아니라면 erase코드 )
#include <iostream> #include <list> using namespace std; int T; string str; list<char> ans; int main(void) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> T; while(T--) { cin >> str; list<char>::iterator itr = ans.end(); for( int i=0; i<str.length(); i++) { if( str[i] == '<' ) { if(itr == ans.begin()) continue; itr--; } else if(str[i] == '>') { if(itr == ans.end()) continue; itr++; } else if(str[i] == '-') { if(itr == ans.begin()) continue; itr = ans.erase(--itr); } else { ans.insert(itr, str[i]); } } for(auto it = ans.begin(); it != ans.end(); it++) cout << *it; cout << '\n'; ans.clear(); } }
'백준코딩일기' 카테고리의 다른 글
10828) 스택 c++ (0) 2020.11.05 1158) 요세푸스 문제 (0) 2020.11.05 1406) 에디터 c++ (0) 2020.11.05 5430) AC c++ (0) 2020.11.05 1021) 회전하는 큐 c++ (0) 2020.11.04