Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

21-alstjr7437 #74

Merged
merged 3 commits into from
May 10, 2024
Merged

21-alstjr7437 #74

merged 3 commits into from
May 10, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Apr 6, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

์ง‘ํ•ฉ
๊ทธ๋ƒฅ ๋‹จ์ˆœํ•œ ์ง‘ํ•ฉ ๋ฌธ์ œ์ธ๋ฐ (์‚ฌ์‹ค ์‹œ๊ฐ„๊ณผ ๋ชธ์˜ ๋ฌธ์ œ๋กœ ๋น„ํŠธ๋งˆ์Šคํ‚น ์ดํ•ด๋งŒ ํ•˜๋Š” ๋ฌธ์ œ๋กœ ์„ ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.)

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

ํ•ด๊ฒฐ์€ 10๋ถ„ 3๊ฐ€์ง€ ํ’€์ด๋กœ ํ•˜๋Š”๋ฐ 25๋ถ„ ์ •๋„..?




๐Ÿ“œ ์ „์ฒด ๋ฌธ์ œ ํ’€์ด

์ผ๋‹จ ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ์„ ์ •ํ•˜๋ฉด์„œ ๊ตฌํ˜„(set ์ด์šฉ), ๋น„ํŠธ๋งˆ์Šคํฌ๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ์ง‘ํ•ฉ์œผ๋กœ ๋ฌธ์ œ ํ’€๊ธฐ
  2. ๋น„ํŠธ๋งˆ์Šคํฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฌธ์ œ ํ’€๊ธฐ
  3. ๋น„ํŠธ๋งˆ์Šคํฌ๋กœ ๋ฌธ์ œ ํ’€๊ธฐ





1๏ธโƒฃ ์ง‘ํ•ฉ

๊ทธ๋ƒฅ python์—์„œ ์ง‘ํ•ฉ(set)์„ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„์œผ๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.



set์˜ add, discard, in ๋“ฑ์„ ์‚ฌ์šฉํ•ด์„œ ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค!


import sys

input = sys.stdin.readline

m = int(input())

S = set()

for _ in range(m):
    temp = input().split()
    
    if temp[0] == "add":
        S.add(int(temp[1]))
    if temp[0] == "remove":
        S.discard(int(temp[1]))
    if temp[0] == "check":
        if int(temp[1]) in S:
            print(1)
        else :
            print(0)
    if temp[0] == "toggle":
        if int(temp[1]) in S:
            S.discard(int(temp[1]))
        else:
            S.add(int(temp[1]))
    if temp[0] == "all":
        S = set([i for i in range(1, 21)])
    if temp[0] == "empty":
        S = set()






2๏ธโƒฃ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•œ ๋น„ํŠธ๋งˆ์Šคํฌ ์‚ฌ์šฉ

์ฃผ์˜ : ์ €๊ฐ€ ๋ฆฌ์ŠคํŠธ๋กœ ํ•œ ๋ฐฉ๋ฒ•์ด ๋น„ํŠธ๋งˆ์Šคํฌ๊ฐ€ ์•„๋‹ ์ˆ˜๋„ ์žˆ๊ธดํ•œ๋ฐ ์ €๋Š” ๋น„ํŠธ๋งˆ์Šคํฌ์™€ ๋น„์Šทํ•˜๋‹ค๊ณ  ์ƒ๊ฐ..

์ผ๋‹จ ์ง‘ํ•ฉ์— ์›์†Œ๊ฐ€ 1~20๊นŒ์ง€๋งŒ ์žˆ์œผ๋ฉด ๋์Šต๋‹ˆ๋‹ค.
๊ทธ๋ž˜์„œ 20๊ฐœ์˜ 0์œผ๋กœ ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋ถ€๋ถ„์—์„œ add๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ํ•ด๋‹น ์ž๋ฆฌ๋ฅผ 1๋กœ ๊ต์ฒดํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.


import sys

input = sys.stdin.readline

m = int(input())

result = [0] * 21

for _ in range(m):
    cmd = input().split()

    if cmd[0] == "add":
        result[int(cmd[1])] = 1

    if cmd[0] == "remove":
        result[int(cmd[1])] = 0

    if cmd[0] == "check":
        if result[int(cmd[1])] == 1:
            print(1)
        else :
            print(0)

    if cmd[0] == "toggle":
        if result[int(cmd[1])] == 1:
            result[int(cmd[1])] = 0
        else :
            result[int(cmd[1])] = 1

    if cmd[0] == "all":
        result = [1] * len(result)

    if cmd[0] == "empty":
        result = [0] * len(result)






3๏ธโƒฃ ๋น„ํŠธ๋งˆ์Šคํฌ result๋งŒ ์ด์šฉํ•ด์„œ

๋น„ํŠธ๋งˆ์Šคํฌ ํ•˜๋ฉด 101 -> 5 ์ด๋Ÿฐ์‹์œผ๋กœ ๊ฐ ์ด์ง„์ˆ˜ ํ‘œํ˜„์œผ๋กœ ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๊ธฐ๋ฒ•์ด๋ฏ€๋กœ


  • add๊ฐ€ ๋“ค์–ด์˜ค๋ฉด or ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ง„ํ–‰
  • remove๊ฐ€ ๋“ค์–ด์˜ค๋ฉด and ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ง„ํ–‰
  • toggle์€ xor ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ง„ํ–‰
  • check๋Š” ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋กœ ์ฒดํฌํ•˜๊ธฐ
  • all์€ 1~20๊นŒ์ง€ ์ „๋ถ€ ๋น„ํŠธ๊ฐ€ 1๋กœ ๋ฐ”๋€Œ๋„๋ก
  • empty๋Š” 0์œผ๋กœ
import sys

input = sys.stdin.readline

m = int(input())

result = 0

for _ in range(m):
    cmd = input().split()

    if cmd[0] == "add":
        result |= (1 << int(cmd[1]))  # ํ•ด๋‹น ๋น„ํŠธ๋ฅผ 1๋กœ ์„ค์ •

    if cmd[0] == "remove":
        result &= ~(1 << int(cmd[1]))  # ํ•ด๋‹น ๋น„ํŠธ๋ฅผ 0์œผ๋กœ ์„ค์ •

    if cmd[0] == "check":
        if result & (1 << int(cmd[1])):
            print(1)
        else:
            print(0)

    if cmd[0] == "toggle":
        result ^= (1 << int(cmd[1]))  # ํ•ด๋‹น ๋น„ํŠธ๋ฅผ ๋ฐ˜์ „

    if cmd[0] == "all":
        result = (1 << 21) - 1  # ๋ชจ๋“  ๋น„ํŠธ๋ฅผ 1๋กœ ์„ค์ •

    if cmd[0] == "empty":
        result = 0  # ๋ชจ๋“  ๋น„ํŠธ๋ฅผ 0์œผ๋กœ ์„ค์ •

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

ํ•˜๋‚˜์˜ ๋ฌธ์ œ๋ฅผ ํ†ตํ•ด์„œ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ํ’€์ด๋ฒ•์ด ๋‚˜์˜ค๊ณ  ๊ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„์ด ๋‹ค๋ฅธ๊ฑธ ํ™•์ธํ–ˆ์Šต๋‹ˆ๋‹ค.

set ์ž๋ฃŒ๊ตฌ์กฐ๊ฐ€ ์ œ์ผ ์˜ค๋ž˜ ๊ฑธ๋ ธ๊ณ  list์™€ ๋น„ํŠธ๋งˆ์Šคํฌ๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•์ด ๋น„์Šทํ–ˆ์Šต๋‹ˆ๋‹ค.(list๊ฐ€ ๊ฐ€๋” ๋”๋นจ๋ž์Šต๋‹ˆ๋‹ค..)

๋น„ํŠธ์—ฐ์‚ฐ์ž -> 3732
list -> 3632
set ์ž๋ฃŒ๊ตฌ์กฐ -> 5080
image

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

๋น„ํŠธ๋งˆ์Šคํ‚น์ธ์ง€ ๋ชจ๋ฅด๊ณ  ์ฒ˜์Œ์— ์„ธ๋ฒˆ์˜ ์‹œ๊ฐ„์ดˆ๊ณผ๋ฅผ ๋งŒ๋‚ฌ๋Š”๋ฐ์š”..
์–ด๋–ป๊ฒŒ๋“  ์ค„์—ฌ๋ณด๋ ค๊ณ  hashSet ์„ ์“ฐ๊ณ  ํ–ˆ๋Š”๋ฐ ๊ฒฐ๊ตญ ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    var set = hashSetOf<Int>()

    for (i in 0 until n) {
        val input = br.readLine().split(" ")

        if (input[0] == "all") {
            set = (1..20).toHashSet()
            continue
        }

        if (input[0] == "empty") {
            set.clear()
            continue
        }

        val number = input[1].toInt()
        when (input[0]) {
            "add" -> set.add(number)
            "check" -> if (set.contains(number)) println(1) else println(0)
            "remove" -> set.remove(number)
            "toggle" -> if (!set.remove(number)) set.add(number)
        }
    }
}

kotlin ๋น„ํŠธ๋งˆ์Šคํ‚น ์ž˜ ๋ชฐ๋ผ์„œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜๋ฉด์„œ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()

    var bit = 0
    val sb = StringBuilder()

    for (i in 0 until n) {
        val input = br.readLine().split(" ")
        var number = 0

        if (input[0] == "all") {
            bit = bit or 0.inv()
            continue
        }

        if (input[0] == "empty") {
            bit = 0
            continue
        }

        number = input[1].toInt()
        when (input[0]) {
            "add" -> bit = bit or (1 shl number - 1)
            "check" -> {
                val temp = bit and (1 shl number - 1)
                sb.append(if (temp != 0) "1" else "0").append("\n")
            }

            "remove" -> bit = bit and (1 shl number - 1).inv()
            "toggle" -> bit = bit xor (1 shl number - 1)
        }
    }
    println(sb.toString())
}

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

import sys

m = int(sys.stdin.readline())
S = set()

for _ in range(m):
    temp = sys.stdin.readline().strip().split()
    if len(temp) == 1:
        if temp[0] == "all":
            S = set([i for i in range(1, 21)])
        else:
            S = set()
    else:
        func, x = temp[0], temp[1]
        x = int(x)
        if func == "add":
            S.add(x)
        elif func == "remove":
            S.discard(x)
        elif func == "check":
            print(1 if x in S else 0)
        elif func == "toggle":
            if x in S:
                S.discard(x)
            else:
                S.add(x)

@alstjr7437
Copy link
Member Author

import sys

m = int(sys.stdin.readline())
S = set()

for _ in range(m):
    temp = sys.stdin.readline().strip().split()
    if len(temp) == 1:
        if temp[0] == "all":
            S = set([i for i in range(1, 21)])
        else:
            S = set()
    else:
        func, x = temp[0], temp[1]
        x = int(x)
        if func == "add":
            S.add(x)
        elif func == "remove":
            S.discard(x)
        elif func == "check":
            print(1 if x in S else 0)
        elif func == "toggle":
            if x in S:
                S.discard(x)
            else:
                S.add(x)

์–ดํ—ˆ ์ด์–‘๋ฐ˜์•„ ์ด๊ฒŒ ๋ฆฌ๋ทฐ์•ผ?
๊ทธ๋ƒฅ ๋ณธ์ธ ์ฝ”๋“œ ์˜ฌ๋ฆฐ๊ฑฐ์ž–์•„

@wkdghdwns199
Copy link
Collaborator

๋น„์Šทํ•˜๊ฒŒ ๋‚˜์™€์„œ ๊ทธ๋ƒฅ ์˜ฌ๋ ธ๋„ค์š”..! ์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜ข

@tgyuuAn tgyuuAn merged commit 1e75787 into main May 10, 2024
@tgyuuAn tgyuuAn deleted the 21-alstjr7437 branch May 10, 2024 13:13
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.

4 participants