BOJ : 5397 (키로거)
문제
Code
#include <iostream>
#include <list>
using namespace std;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
while(N--)
{
list<char> L;
list<char>::iterator cursor=L.begin();
string S;
cin >> S;
for(auto a : S)
{
switch(a)
{
case '-':
{
if(cursor != L.begin())
{
cursor = L.erase(--cursor);
}
break;
}
case '<':
{
if(cursor != L.begin())
cursor--;
break;
}
case '>':
{
if(cursor != L.end())
cursor++;
break;
}
default :
{
L.insert(cursor,a);
break;
}
}
}
for(auto a : L)
cout << a ;
cout <<'\n';
}
}
- cursor = L.begin(); -> L.insert(cursor, 3) 하게되면
it는 L.end()와 같은 위치에 있다.
- cursor = L.erase(cursor)
삭제 후 cursor의 값을 바꿔주지 않으면 cursor는 삭제된 값을 가지고 있다! 왜그러냐;
- list.end()는 마지막 요소 다음을 가리킨다!(빈 곳)
Author And Source
이 문제에 관하여(BOJ : 5397 (키로거)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://velog.io/@neity16/BOJ-5397-키로거
저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
#include <iostream> #include <list> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; while(N--) { list<char> L; list<char>::iterator cursor=L.begin(); string S; cin >> S; for(auto a : S) { switch(a) { case '-': { if(cursor != L.begin()) { cursor = L.erase(--cursor); } break; } case '<': { if(cursor != L.begin()) cursor--; break; } case '>': { if(cursor != L.end()) cursor++; break; } default : { L.insert(cursor,a); break; } } } for(auto a : L) cout << a ; cout <<'\n'; } }
- cursor = L.begin(); -> L.insert(cursor, 3) 하게되면
it는 L.end()와 같은 위치에 있다.- cursor = L.erase(cursor)
삭제 후 cursor의 값을 바꿔주지 않으면 cursor는 삭제된 값을 가지고 있다! 왜그러냐;- list.end()는 마지막 요소 다음을 가리킨다!(빈 곳)
Author And Source
이 문제에 관하여(BOJ : 5397 (키로거)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@neity16/BOJ-5397-키로거저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)