[SWEA, Python] [S/W 문제해결 기본] 8일차 - 암호문3

김서영·2022년 7월 24일

코딩테스트 스터디

목록 보기
7/11

문제

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=3&contestProbId=AV14zIwqAHwCFAYD&categoryId=AV14zIwqAHwCFAYD&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=PYTHON&select-1=3&pageSize=10&pageIndex=10&&&&&&&&&&

해결

우선 입력은 평소처럼 list형, int형 등으로 받아준다.
사실 구현은 어렵지 않은데, 테스트 케이스가 너무 많아서 눈알 아팠음...
각 명령어들마다 idx 값을 잘 조정하여서, 암호문 list에 잘 넣거나 빼기만 하면 됨!

코드

import sys
sys.stdin = open("input.txt", "r")

T = 10
for test_case in range(1, T + 1):
    n = int(input())
    code = list(input().split())
    x = int(input())
    xcode = list(input().split())
    ptr = 0
    
    for i in range(x):
        typ = xcode[ptr]
        
        if typ == "I":
            # 위치 선정
            ptr += 1
            j = int(xcode[ptr])

            # 얼마나 삽입할 건지
            ptr += 1
            cnt = int(xcode[ptr])

            for i in range(cnt):
                ptr += 1
                code.insert(j,xcode[ptr])
                j += 1

            ptr += 1

        elif typ == "D":
            ptr += 1
            j = int(xcode[ptr])-1

            ptr += 1
            cnt = int(xcode[ptr])

            for i in range(cnt):
                del code[j]

            ptr += 1

        elif typ == "A":
            ptr += 1
            cnt = int(xcode[ptr])

            for i in range(cnt):
                ptr += 1
                newcode = xcode[ptr]
                code.append(newcode)
            ptr += 1
            
    print("#"+str(test_case),end=" ")
    for i in range(10):
        print(code[i],end=" ")
    print()
profile
하지만 저는 이겨냅니다. 김서영이죠?

0개의 댓글