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

19-alstjr7437 #67

Merged
merged 6 commits into from
Apr 10, 2024
Merged

19-alstjr7437 #67

merged 6 commits into from
Apr 10, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Mar 27, 2024

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

IOIOI

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

45๋ถ„
์ด๋ ‡๊ฒŒ ๊ฑธ๋ฆด ์‹œ๊ฐ„์ด ์•„๋‹ˆ์˜€๋Š”๋ฐ Swift์— ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ๋งŽ์ด ๋ฐœ๊ฒฌํ•จ..
๊ทธ๋ฆฌ๊ณ  50์  ๋ถ€๋ถ„์—์„œ ๋ง‰ํ˜€์„œ ์•„์ด๋””์–ด๋ฅผ ๊ณ„์† ์ƒ๊ฐํ•จ.

๐Ÿ“œ ๋ฌธ์ œ ์„ค๋ช…

  1. IOIOI์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณต๋˜๋Š” ๋ฌธ์ž s๊ฐ€ ์žˆ์Œ
  2. n์ด ์ฃผ์–ด์ง
  3. IOI์ด๋ ‡๊ฒŒ ๊ต๋Œ€๋กœ ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ๊ฒฐ๊ณผ์— +1์„ํ•จ.
    1. n = 1 -> P1 -> IOI
    2. n = 2 -> P2 -> IOIOI
  4. s์— Pn์ด ์–ผ๋งˆ๋‚˜ ์žˆ๋Š”์ง€ ๋ณด๊ณ  ๊ฒฐ๊ณผ ์ถœ๋ ฅ





1๏ธโƒฃ 50์  ๋งž์€ ์ฝ”๋“œ

์ €๋Š” ์ฒ˜์Œ์—๋Š” ๊ทธ๋ƒฅ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ์ƒ๊ฐํ•˜์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. p๋ผ๋Š” ๋ฌธ์ž์—ด์— n๋งŒํผ OI๋ฅผ ์ถ”๊ฐ€
  2. s ๋ฌธ์ž์—ด์„ ํ•˜๋‚˜์”ฉ ์ธ๋ฑ์Šค 0๋ถ€ํ„ฐ ์ตœ๋Œ€ ๊ธธ์ด๊นŒ์ง€ i๋กœ ๋ฐ˜๋ณต๋ฌธ
  3. s ๋ฌธ์ž์—ด๊ณผ p๋ผ๋Š” ๋ฌธ์ž์—ด์ด ๋˜‘๊ฐ™์œผ๋ฉด ๊ฒฐ๊ณผ +1



์œ„์™€ ๊ฐ™์ด ์ƒ๊ฐํ–ˆ๊ณ  ์ฝ”๋“œ๋ฅผ ์งœ๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค

let n = Int(readLine()!)!
let m = Int(readLine()!)!

let s = readLine()!.map{$0}

var result = 0
var p = "I"

for _ in 0 ..< n{
    p += "OI"
}


for i in 0..<m-p.count+1{
    if String(s[i..<i+p.count]) == p{
        result += 1
    }
}

print(result)

ํ•˜์ง€๋งŒ 50์ ์„ ๋งž๊ธธ๋ž˜ ์ž˜ ์ƒ๊ฐ์„ ํ•ด๋ณด๋‹ˆ ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด ๋ชจ๋“  s์˜ ๋ฌธ์ž์—ด์„ ์ธ๋ฑ์Šค๋กœ ๋‹ค ๋„๋Š”๊ฑฐ๋ผ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๊ฒ ๋”๋ผ๊ณ ์š”!






๐Ÿ’ฏ ์ฝ”๋“œ

๊ทธ๋ž˜์„œ ์ƒ๊ฐํ•˜๊ณ  ์ฐธ๊ณ ํ•œ ๋ถ€๋ถ„์ด

  1. ํ˜„์žฌ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•  ์ˆ˜๋ฅผ ๋งŒ๋“ฌ -> cur
  2. s์˜ cur ~ cur +2 ๊ฐ€ IOI๋ฉด -> ์˜ˆ 0๋ถ€ํ„ฐ 2๊นŒ์ง€ IOI๋ฉด
    • count ์ถ”๊ฐ€ (count์™€ n ๋น„๊ต ์œ„ํ•ด์„œ
    • cur + 2 ( ์œ„์น˜๋Š” 2์นธ ์ด๋™ํ–ˆ์œผ๋ฏ€๋กœ cur + 2)
  3. count์™€ n์ด ๊ฐ™์€์ง€ ๋น„๊ต
    • n์ด 1์ด๋ฉด IOI์ด๋ฏ€๋กœ ๊ทธ๋Œ€๋กœ result์— + 1
  4. IOI๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์œผ๋ฉด
    • count ๋Š” 0์œผ๋กœ ์ดˆ๊ธฐํ™” IOIOO๋ฉด ๋‹ค์‹œ IOI๋ถ€ํ„ฐ ๋ด์•ผํ•˜๋ฏ€๋กœ 0์œผ๋กœ ์ดˆ๊ธฐํ™”
    • cur += 1 ํ˜„์žฌ์œ„์น˜๋Š” ๊ทธ๋ƒฅ ํ•œ์นธ ๋’ค๋กœ ๋•ก๊น€
      ์œ„ ๋ฒˆํ˜ธ๋ฅผ ์ƒ๊ฐํ•˜๋ฉด์„œ ๊ทธ๋ฆผ์œผ๋กœ ์˜ˆ์ œ๋ฅผ ํ‘œํ˜„ํ•˜๋ฉด




image

์œ„์™€ ๊ฐ™์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋Œ์•„๊ฐ€์„œ
result = 2๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ง„ํ–‰ํ•˜๊ฒŒ ๋˜๋ฉด
IOI๊ฐ€ ๋‚˜์™”์„๋•Œ ํ˜„์žฌ ์œ„์น˜๋ฅผ 2์นธ ์•ž์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„์ด ์ค„์–ด๋“œ๋Š” ๋ถ€๋ถ„์„ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.


์›๋ž˜๋Š” IOI๊ฐ€ ๋‚˜์™€๋„ ์ „์ฒด๋กœ ๊ฐ™์€์ง€ ํ™•์ธ๋งŒํ•˜๊ณ  ๋ฐ”๋กœ ์ธ๋ฑ์Šค๊ฐ€ +1๋งŒ ๋˜์ง€๋งŒ ์ด๋Ÿฌํ•œ ๋ถ€๋ถ„์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ํ™•์‹คํžˆ ์‹œ๊ฐ„ ๋ณต์žก๋„๊ฐ€ ์ค„์–ด๋“œ๋Š” ๋ถ€๋ถ„์„ ํ™•์ธํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค!



์œ„ ์‚ฌ์ง„์˜ ์˜ˆ์ œ๋กœ ์ง„ํ–‰ํšŸ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด

  1. 50์  ๋ฐ›์€ ์ฝ”๋“œ์˜ ์ง„ํ–‰ ํšŸ์ˆ˜ -> 0,1,2,3,4,5,6,7,8 -> 9
  2. 100์  ๋ฐ›์€ ์ฝ”๋“œ์˜ ์ง„ํ–‰ ํšŸ์ˆ˜ -> 0,1,2,4,6,8,9 -> 7






๐Ÿ’ป ์ตœ์ข… ์ฝ”๋“œ

let n = Int(readLine()!)!
let m = Int(readLine()!)!

let s = readLine()!.map{$0}

var result = 0
var cur = 0
var count = 0

while cur < m-2{
    if String(s[cur...cur + 2]) == "IOI"{
        count += 1
        cur += 2
        if count == n{
            result += 1
            count -= 1
        }
    } else {
        count = 0
        cur += 1
    }
}

print(result)






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

1๋ฒˆ ์•„์ด๋””์–ด๋„ ์›๋ž˜ ์งœ๋Š”๋ฐ ํ•œ 7๋ถ„์ด๋ฉด ๋˜๋Š” ์•„์ด๋””์–ด์˜€์ง€๋งŒ Swift์–ธ์–ด๋กœ ์งœ๋ณด๋‹ˆ ๋‘๋ฐฐ๋Š” ๊ฑธ๋ฆฐ ๊ฒƒ ๊ฐ™๋‹ค.


์—ฌ๋Ÿฌ๊ฐ€์ง€ nil๋กœ ์™”์„๋•Œ ์ฒ˜๋ฆฌ, ๋ฌธ์ž์—ด์„ ๋ฐ›์•˜์„๋•Œ map์„ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์•ผํ•จ(String์€ Index ์ ‘๊ทผ์ด ์•ˆ๋จ)
๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ String๋ฐฐ์—ด์„ String์œผ๋กœ ํƒ€์ž… ๋ฐ”๊ฟ”์ค˜์„œ ๋น„๊ต๋ฅผ ํ•˜๋Š”๊ฒŒ ๋„ˆ๋ฌด ๊ท€์ฐฎ์•˜๋‹ค...


๊ทธ๋ฆฌ๊ณ  ๋ฐฑ์ค€ ๊ธฐ๋ณธ์–ธ์–ด ๋ณ€๊ฒฝ๋„ ์žŠ์ง€๋ง์ž..
image

@alstjr7437 alstjr7437 self-assigned this Mar 27, 2024
@alstjr7437 alstjr7437 marked this pull request as draft March 27, 2024 08:28
Copy link
Member

@fnzksxl fnzksxl 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

inpur = sys.stdin.readline

N = int(input())
m = int(input())
lst = input().strip()

cur, ans, cnt = 0, 0, 0

while cur < m-1:
    if  lst[cur:cur+3] == 'IOI':
        cnt += 1
        cur += 2
        if cnt == n:
            ans += 1
            cnt -= 1
    else:
        cnt = 0
        cur += 1

print(ans) 

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.

ํ’€๊ธด ํ’€์—ˆ๋Š”๋ฐ ๋จธ๋ฆฌ๊ฐ€ ์–ด์ง€๋Ÿฝ๋„ค์š”

import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    val m = br.readLine().toInt()
    val s = br.readLine()
    var answer = 0
    var i = 0
    var count = 0

    while (i < m - 2) {
        if (s.substring(i, i + 3) == "IOI") {
            i += 2
            count++
            if (count == n) {
                answer++
                count--
            }
        } else {
            i++
            count = 0
        }
    }
    println(answer)
}

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
input = sys.stdin.readline
N = int(input())
M = int(input())
S = input()

current_idx = 0
count = 0
result =0
while current_idx < M-2 :
    if S[current_idx:current_idx+3] == 'IOI':
        count+=1
        current_idx+=2
        if count == N :
            count -=1
            result +=1
        
    else :
        count=0
        current_idx+=1

print(result)

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