[SWEA] 암호문3 - list

이성현·2021년 10월 5일
0

SWEA

목록 보기
1/5

오늘부터 문제 푼 코드를 velog에 남겨놓기로 했다. 화이팅!
암호문3

난이도 : MH

배운 것

STL 활용 list 사용하기
list의 가장 큰 특징은 순차열 중간에 원소를 삽입,삭제해도 수행 성능이 상수 시간 복잡도이기 때문에 해당 문제에 적합하다.

auto itr=li.begin(); //list 형태에 접근할 때 iterator을 쓰자.
li=splice(itr,temp); //itr위치에 temp를 삽입한다.
itr=li.erase(itr); // erase()함수는 삭제 뒤 itr을 return 한다.

#include <iostream>
#include <list>
using namespace std;

int N;
int target;
int commandNo;
char cType;
list <int> li;
auto itr = li.begin();
int main() {
	for (int tc = 1; tc <= 10; tc++) {
		li.clear();//리스트 초기화
		itr = li.begin();
		scanf("%d", &N);
		for (int i = 0; i < N; i++) {
			scanf("%d", &target);
			li.push_back(target);
		}
		scanf("%d", &commandNo);
		for (int cc = 0; cc < commandNo; cc++) {
			scanf(" %c", &cType);
			int x, y, s;
			if (cType == 'I') {

				itr = li.begin();
				list<int> temp;

				scanf("%d %d", &x, &y);//앞에서부터 x의 위치 바로 다음에 y개 숫자 삽입
				for (int i = 0; i < x; i++) itr++;
				for (int i = 0; i < y; i++) {
					scanf("%d", &s);
					temp.push_back(s);
				}
				li.splice(itr, temp); //itr위치에 temp끼워넣기
			}
			else if (cType == 'D') {
				itr = li.begin();
				scanf("%d %d", &x, &y);//앞에서부터 x의 위치 바로 다음부터 y개 숫자 삭제
				for (int i = 0; i < x; i++)itr++;
				for (int i = 0; i < y; i++) itr = li.erase(itr); //erase는 itr을 return 한다
			}
			else if (cType == 'A') {
				scanf("%d", &y);//암호문 맨 뒤에 y개 숫자 덧붙인다.
				for (int i = 0; i < y; i++) {
					scanf("%d", &s);
					li.push_back(s);
				}
			}
		}
		
		itr = li.begin();
		printf("#%d ", tc);
		for (int i = 0; i < 10; i++) {
			printf("%d ", *(itr++));
		}
        printf("\n");

	}

	return 0;
}
profile
삼성전자 C-Lab 21기 Creative Leader SW개발자 (쪼랩)

1개의 댓글

comment-user-thumbnail
2021년 10월 6일

화이팅!

답글 달기