Skip to content

Conversation

@froglike6
Copy link
Collaborator

@froglike6 froglike6 commented May 11, 2025

πŸ”— 문제 링크

AC

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

20λΆ„

✨ μˆ˜λ„ μ½”λ“œ

μˆ˜λ„ μ½”λ“œ

    T ← μ •μˆ˜ μž…λ ₯            β–Έ ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€ 개수

    T 번 반볡
        func ← λ¬Έμžμ—΄ μž…λ ₯          β–Έ R κ³Ό D 둜 이루어진 λͺ…λ Ήμ–΄
        n    ← μ •μˆ˜ μž…λ ₯            β–Έ λ°°μ—΄ μ›μ†Œ 수

        λ§Œμ•½ n = 0 이면
            arr ← 빈 deque 생성
            빈 λ°°μ—΄ 라인 μ†Œλͺ¨        β–Έ "[]"
        κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄
            line ← λ¬Έμžμ—΄ μž…λ ₯      β–Έ "[1,2,3]" ν˜•νƒœ
            nums ← line 의 첫·끝 문자 제거 ν›„ "," 둜 λΆ„λ¦¬ν•˜μ—¬ μ •μˆ˜λ‘œ λ³€ν™˜
            arr ← nums 둜 deque 생성

        rev ← False                 β–Έ λ’€μ§‘κΈ° μƒνƒœ ν”Œλž˜κ·Έ
        err ← False                 β–Έ μ—λŸ¬ λ°œμƒ μ—¬λΆ€

        func 의 각 문자 i 에 λŒ€ν•΄
            λ§Œμ•½ i = 'R' 이면
                rev ← not rev       β–Έ λ’€μ§‘κΈ° ν† κΈ€
            κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄           β–Έ i = 'D' (μ‚­μ œ)
                λ§Œμ•½ arr κ°€ λΉ„μ–΄ 있으면
                    "error" 좜λ ₯
                    err ← True
                    루프 νƒˆμΆœ
                μ•„λ‹ˆκ³  rev κ°€ True 이면
                    arr.pop()       β–Έ λ’€μ—μ„œ 제거
                μ•„λ‹ˆλ©΄
                    arr.popleft()   β–Έ μ•žμ—μ„œ 제거

        μ—λŸ¬κ°€ λ°œμƒν•˜μ§€ μ•Šμ•˜λ‹€λ©΄
            λ§Œμ•½ rev κ°€ True 이면
                arr.reverse()       β–Έ μ΅œμ’… λ’€μ§‘κΈ°
            κ²°κ³Ό λ¬Έμžμ—΄ ← "[" + arr μ›μ†Œλ₯Ό "," 둜 μ—°κ²° + "]"
            κ²°κ³Ό λ¬Έμžμ—΄ 좜λ ₯

이 λ¬Έμ œλŠ” ν…ŒμŠ€νŠΈ μΌ€μ΄μŠ€λ§ˆλ‹€ 배열이 μ£Όμ–΄μ§€κ³ , κ·Έ 배열을 μ£Όμ–΄μ§„ λͺ…령에 따라 λ’€μ§‘κ±°λ‚˜ λ²„λ¦¬λŠ” λ¬Έμ œμž…λ‹ˆλ‹€.
μ²˜μŒμ—λŠ” 배열에 직접 λ„£κ³  reverseν•¨μˆ˜λ‘œ 직접 λ’€μ§‘μ„κΉŒ ν•˜λ‹€κ°€, μ‹œκ°„ μ΄ˆκ³Όκ°€ λ‚  것 κ°™μ•„ λ‹€λ₯Έ 방법을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.

revλΌλŠ” λ³€μˆ˜λ₯Ό λ”°λ‘œ μ„ μ–Έν•΄μ„œ, λ’€μ§‘λŠ” λͺ…령이 λ“€μ–΄μ˜€λ©΄ rev = not rev을 톡해 ν‘œμ‹œλ§Œ ν•˜κ²Œ ν–ˆμŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ arr이 True이면 pop을 ν•˜κ³ (λ’€μ§‘μ–΄μ‘ŒμœΌλ©΄ μ™Όμͺ½μ΄ 였λ₯Έμͺ½μ΄ 됨!), μ•„λ‹ˆλ©΄ popleftλ₯Ό ν•˜κ²Œ ν•΄μ„œ 결과적으둜 O(1)λ§Œμ— λ²„λ¦¬λŠ” λ™μž‘μ„ ν•  수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

μ΄λ ‡κ²Œ 연산을 ν•˜κ³ , λ§ˆμ§€λ§‰μ— λ’€μ§‘μ–΄μ‘ŒλŠ”μ§€ ν™•μΈν•˜μ—¬ μƒνƒœμ— 맞게 dequeλ₯Ό 좜λ ₯ν–ˆμŠ΅λ‹ˆλ‹€.


μ²˜μŒμ— ν’€ λ•Œ 빈 λ°°μ—΄ 처리λ₯Ό μ•ˆ ν•˜κ³  ν’€μ—ˆλ”λ‹ˆ μ—λŸ¬κ°€ λ–΄μŠ΅λ‹ˆλ‹€ γ…Žγ…Ž.. 이 점 κ³ λ €ν•΄μ„œ 문제λ₯Ό ν’€μ–΄μ•Όν•©λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

err = False
for i in func:
if i == "R":
rev = not rev
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맀우 μ‚¬μ†Œν•œ 건데

rev ^= True

0, 1을 ν”Œλ¦½ν•˜λŠ” κ±°λ‹€ λ³΄λ‹ˆ xor 1 ν•΄μ£ΌλŠ” κ±Έλ‘œλ„ κ°€λŠ₯ν•˜κΈ΄ ν•©λ‹ˆλ‹€ γ…Žγ…Ž

Copy link
Contributor

@dohyeondol1 dohyeondol1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AC....μ§„μ§œ ν’€λ©΄μ„œ 이게 λ§žλ‚˜ 싢도둝 κΉ”λ”ν•˜μ§€ μ•Šμ€ μ½”λ“œκ°€ λ‚˜μ™€μ„œ ACλ₯Ό λΆ€λ₯΄κ²Œλ˜λŠ”...

저도 κ·Έλƒ₯ λ°°μ—΄ 전체λ₯Ό λ’€μ§‘μ–΄ 풀어보렀고 ν–ˆλŠ”λ° λ°”λ‘œ ν‹€λ ΈμŠ΅λ‹ˆλ‹€ γ…Ž
Dκ°€ "첫번째 수λ₯Ό λ²„λ¦¬λŠ” ν•¨μˆ˜"λΌλŠ” 점에 μ§‘μ€‘ν•˜λ‹ˆ κ·Έμ œμ•Ό 덱을 μ‚¬μš©ν•΄μ•Όκ² λ‹€λŠ” 생각이 λ“€λ”λΌκ΅¬μš”..

C++
#include <iostream>
#include <vector>
#include <deque>
#include <string>
using namespace std;

int main() {
    cin.tie(nullptr)->sync_with_stdio(false);

    int T;
    cin >> T;
    while(T--) {
        string order;
        int N;
        cin >> order >> N;

        deque<int> dq;
        char ch;
        cin >> ch;
        for(int i = 0; i < N; i++) {
            int x;
            cin >> x;
            dq.push_back(x);
            if(i != N - 1) cin >> ch;
        }
        cin >> ch;

        bool isReverse = false, isError = false;
        for(char c : order) {
            if(c == 'R') {
                isReverse = !isReverse;
            } else if(c == 'D') {
                if(dq.empty()) {
                    isError = true;
                    break;
                }
                if(!isReverse) dq.pop_front();
                else dq.pop_back();
            }
        }

        if(isError) {
            cout << "error" << '\n';
        } else {
            cout << '[';
            if(!dq.empty()) {
                if(!isReverse) {
                    for(size_t i = 0; i < dq.size(); i++) {
                        cout << dq[i];
                        if(i != dq.size() - 1) cout << ',';
                    }
                } else {
                    for(size_t i = dq.size(); i-- > 0;) {
                        cout << dq[i];
                        if(i != 0) cout << ',';
                    }
                }
            }
            cout << "]" << '\n';
        }
    }

    return 0;
}

μž…λ ₯에 [ 와 , λ₯Ό 생각 λͺ»ν–ˆλ‹€κ°€ 틀리기도 ν–ˆμŠ΅λ‹ˆλ‹€.. 방심은 금물..

Copy link
Collaborator

@hadongun hadongun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

맀번 R이 λ‚˜μ˜¬ λ•Œλ§ˆλ‹€ λ’€μ§‘μœΌλ‹ˆ μ‹œκ°„μ΄ 맀우 κ±Έλ¦¬λŠ”κ΅°μš”

λ‹€λ“€ ν•˜μ‹  λ°©λ²•μ²˜λŸΌ bool νƒ€μž…μ˜ reverse λ³€μˆ˜λ₯Ό 톡해 λ’€μ§‘μ–΄μ‘ŒλŠ”μ§€, μ•„λ‹Œμ§€λ₯Ό 확인해
pop, popleftλ₯Ό 톡해 ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€..!

점점 λ¬Έμ œλ“€μ˜ λ‚œμ΄λ„κ°€ λ†’μ•„μ§ˆμˆ˜λ‘ κ°„κ²°ν•˜κ³  효율적인 μ½”λ“œλ₯Ό μš”κ΅¬ν•˜λŠ” 것 κ°™λ„€μš”

파이썬

import sys
from collections import deque

t = int(input())
for _ in range(t):
    is_reverse = False
    implement = sys.stdin.readline().strip()
    
    n = int(input())
    arr = sys.stdin.readline().strip()
    arr = arr[1:-1]
    
    if arr == '':
        arr = deque()
    else:
        arr = list(map(str, arr.split(',')))
        arr = deque(arr) 

    for ch in implement:
        if ch == 'R':
            if is_reverse == False:
                is_reverse = True
            else:
                is_reverse = False
        else:
            if arr:
                if is_reverse == True:
                    arr.pop()
                else:
                    arr.popleft()
            else:
                print('error')
                break
    else:  
        if is_reverse == True:
            arr.reverse()
        print('[' + ','.join(map(str, arr)) + ']')

Copy link
Collaborator

@caucsejunseo caucsejunseo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

전체λ₯Ό λ’€μ§‘μ§€ μ•Šκ³  reversed μ΄μš©ν•΄μ„œ front rear둜 μ ‘κ·Όν•˜λŠ” 법! '[,]' 같은 λ¬Έμžμ—΄ μ²˜λ¦¬ν•˜λŠ” 법! λ“± μƒˆλ‘œμš΄ 방법 많이 배우게 된 λ¬Έμ œλ„€μš”. λ¬Έμžμ—΄μ€ 항상 λ²„λ²…μ΄κ²Œ λ˜λŠ”κ±° κ°™μ•„μš”..

C

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <stdbool.h>

#define MAX 100001
int arr[MAX];




int main() 
{

    int T;
    scanf("%d", &T);

    while (T--)
    {
        char p[MAX];
        int n;
        char input[700002];
        scanf("%s", p);
        scanf("%d", &n);
        scanf("%s", input);

        int front = 0 , rear = n - 1;
        bool reversed = false, error = false;

        int idx = 0;
        char* token = strtok(input, "[,]");
        while (token != NULL)
        {
            arr[idx++] = atoi(token);
            token = strtok(NULL, "[,]");
        }

        for (int i = 0; p[i]; i++)
        {
            if (p[i] == 'R')
            {
                reversed = !reversed;
            }
            else if(p[i])
            {
                if (front > rear)
                {
                    error = true;
                    break;
                }
                if (!reversed)
                    front++;
                else
                    rear--;
            }
        }

        if (error)
        {
            printf("error\n");
        }
        else
        {
            printf("[");
            if (!reversed)
            {
                for (int i = front; i < rear+1; i++)
                {
                    printf("%d", arr[i]);
                    if (i < rear)
                    {
                        printf(",");
                    }
                }
            }
            else
            {
                for (int i = rear; i >= front; i--)
                {
                    printf("%d", arr[i]);
                    if (i > front)
                    {
                        printf(",");
                    }
                }
            }
            printf("]\n");
        }


    }

  




    return 0;
}

@9kyo-hwang 9kyo-hwang merged commit 19c1baf into main May 18, 2025
1 check passed
@froglike6 froglike6 deleted the 12-froglike6 branch May 18, 2025 08:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants