Problem Solving/SW Expert Academy

[SWEA] 1230 암호문3 (C++)

LeeJaeJun 2024. 1. 27. 22:01
728x90
반응형

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14zIwqAHwCFAYD

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

리스트와 벡터의 차이점

C++에서 리스트는 원소의 인덱스로 직접 접근이 불가능하여 특정 원소에 접근하기 위해서 처음이나 끝부터 선형 탐색을 해야합니다. 벡터의 경우에는 개별 원소들로 접근이 가능합니다.

출처: https://chanheess.tistory.com/154

 

vector와 list의 차이점

vector 연속적인 메모리. 미래에 들어갈 요소를 위해 선할당을 한다 각 요소는 요소 타입 그자체만큼의 공간을 요구한다 (포인터들을 포함하지 않는다). 당신이 요소를 추가하는 어느 때나, 전체 v

chanheess.tistory.com

 

구현 코드
#include<iostream>
#include<list>

using namespace std;

int main(int argc, char** argv)
{

	int test_case;
	int T = 10;
    int num_of_code;
    int piece_of_code;
    int num_of_command;
    int x;
    int y;
    int s;
    char command;
    list<int> code;
    list<int>::iterator it;
    list<int>::iterator end_it;

	for(test_case = 1; test_case <= T; ++test_case)
	{
        ios_base :: sync_with_stdio(false);
        cin.tie(NULL);
        cout.tie(NULL);
        
        code.clear();

        cin >> num_of_code;

        for(int i = 0; i < num_of_code; i++){
            cin >> piece_of_code;
            code.push_back(piece_of_code);
        }


        cin >> num_of_command;

        for(int i = 0; i < num_of_command; i++){
            cin >> command;
            it = code.begin();
            if(command == 'I'){
                cin >> x >> y;
                advance(it, x);
                for(int j = 0; j < y; j++){
                    cin >> s;
                    code.insert(it, s);
                    advance(it, 1);
                }
            }else if(command == 'D'){
                cin >> x >> y;
                advance(it, x);
                for(int j = 0; j < y; j++){
                    it = code.erase(it);
                }
            }else if(command == 'A'){
                cin >> y;
                for(int j = 0; j < y; j++){
                    cin >> s;
                    code.push_back(s);
                }  
            }     
        }

        cout << "#" << test_case;
        end_it = code.begin();
        advance(end_it, 10);
        for (it = code.begin(); it != end_it; ++it){
            cout << ' ' << *it;
        }
        cout<<'\n';
	}
	return 0;//정상종료시 반드시 0을 리턴해야합니다.
}

리스트는 반복자를 사용해서 처음부터 접근해야 합니다. 그래서 iterator를 begin으로 설정해서 접근하는 방식으로 문제를 풀었습니다.

 

command 'D'를 구현하는 부분에서 처음에는 erase를 쓰면 해당 요소가 사라지고 그 뒤의 요소랑 연결되어 반복자도 그것을 가리킨다고 생각을 해서 code.erase(it)이라고만 했는데 이렇게 하니 요소 삭제가 제대로 업데이트가 되지 않았었습니다.

erase함수는 삭제된 요소 다음 요소를 가리키는 새로운 반복자를 반환하기 때문에 이 값을 업데이트 해주지않으면 반복자 it은 지웠던 그 요소값이 있는 자리를 가리키기 때문에 제대로 작동하지 않기에 삭제된 다음 요소를 가리킬 수 있도록 it = code.erase(it)이라고 수동으로 위치를 이동시켜 주어서 문제를 해결했습니다.

 

728x90
반응형

'Problem Solving > SW Expert Academy' 카테고리의 다른 글

[SWEA] 3000. 중간값 구하기  (0) 2024.02.13
[SWEA] 1251 하나로  (1) 2024.02.06
[SWEA] 1248 공통조상 (c++)  (1) 2024.01.30
[SWEA] 1232 사칙연산(c++)  (0) 2024.01.30
[SWEA] 1233 사칙연산 유효성 검사 (C++)  (1) 2024.01.27