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

81-tgyuuAn #255

Merged
merged 2 commits into from
Dec 1, 2024
Merged

81-tgyuuAn #255

merged 2 commits into from
Dec 1, 2024

Conversation

tgyuuAn
Copy link
Member

@tgyuuAn tgyuuAn commented Nov 14, 2024

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

Cubeeditor

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

30๋ถ„

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

image

๋ฌธ์ œ ์š”์•ฝ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 5,000์ด๊ณ , ๋ฌธ์ž์—ด์€ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, abcabcabc์—์„œ abc๋Š” ์„ธ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋˜, abcabc๋„ ๋‘ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•˜์ง€๋งŒ, abcabca๋Š” ํ•œ ๋ฒˆ ๋‚˜์˜ค๊ธฐ ๋•Œ๋ฌธ์— ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์—†๋‹ค.

๋”ฐ๋ผ์„œ, ๋‘ ๋ฒˆ ์ด์ƒ ๋‚˜์˜ค๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์€ abcabc์ด๊ธฐ ๋•Œ๋ฌธ์—, ์ด ๋ฌธ์ž์—ด์ด ๋‹ต์ด ๋œ๋‹ค.




์ฆ‰, ์ตœ๋Œ€ 5000 ๊ธธ์ด์˜ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ตœ์†Œ 2๋ฒˆ ์ด์ƒ ๋ฐ˜๋ณต๋˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜.




์ด ๋ฌธ์ œ๋ฅผ ์™„ํƒ์œผ๋กœ ํ’€๊ฒŒ ๋œ๋‹ค๋ฉด,

๊ธธ์ด๊ฐ€ 1์ธ ๋ฌธ์ž์—ด 5000๋ฒˆ ํƒ์ƒ‰..

๊ธธ์ด๊ฐ€ 2์ธ ๋ฌธ์ž์—ด 4999๋ฒˆ ํƒ์ƒ‰..

๊ธธ์ด๊ฐ€ 3์ธ ๋ฌธ์ž์—ด 4998๋ฒˆ ํƒ์ƒ‰..

..
..
..

๊ธธ์ด๊ฐ€ 5000์ธ ๋ฌธ์ž์—ด 1๋ฒˆ ํƒ์ƒ‰ ..

$= (5000+1) * (5000 / 2) = ์•ฝ 1์–ต 2์ฒœ 5๋ฐฑ๋งŒ$ ์œผ๋กœ ํƒ€์ž„์•„์›ƒ์ด ๋‚œ๋‹ค.




์˜ˆ๋ฅผ ๋“ค์–ด abcabc๋ผ๋Š” ๋ฌธ์ž์—ด์ด ์กด์žฌํ•  ๊ฒฝ์šฐ,

๊ธธ์ด๊ฐ€ 1์ธ ๋ฌธ์ž์—ด a, b, c, a, b, c ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ ,

๊ธธ์ด๊ฐ€ 2์ธ ๋ฌธ์ž์—ด ab, bc, ca, ab, bc ๋ฅผ ํƒ์ƒ‰ํ•˜๊ณ ,

...

๋งˆ์ง€๋ง‰์œผ๋กœ ๊ธธ์ด๊ฐ€ n์ธ abcabc ๊นŒ์ง€ ์™„ํƒํ•˜๊ฒŒ ๋œ๋‹ค.




๊ทธ๋ž˜์„œ ์ด๋ฅผ ์–ด๋–ป๊ฒŒ ์‹œ๊ฐ„์„ ์ค„์ผ๊นŒ...

๊ณฐ๊ณฐํžˆ ์ƒ๊ฐํ•ด๋ณด๋‹ค๊ฐ€,

์ด๋ถ„ ํƒ์ƒ‰์„ ๋– ์˜ฌ๋ฆฌ๊ฒŒ ๋˜์—ˆ๋‹ค.




์ด๋ถ„ ํƒ์ƒ‰์˜ mid ๊ฐ’์„ ๋ญ๋กœ ํ• ๊ฑด๋ฐ์š”?

-> ํƒ์ƒ‰ํ•˜๋ ค๋Š” ๋ฌธ์ž์—ด์˜ ๊ธธ์ด ๋ฅผ ์ด๋ถ„ํƒ์ƒ‰์˜ mid๊ฐ’์œผ๋กœ ํ•˜๊ฒŒ๋˜๋ฉด,

์ด ์ตœ๋Œ€๊ธธ์ด๊ฐ€ 5000์ด๋ฏ€๋กœ $log(5000) * 5000$ ์„ ํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„

$(log(1000) + log(5)) * 5000$ ์ด๋ฏ€๋กœ ์•ฝ 1๋งŒ 5์ฒœ๋ฒˆ ์•ˆ์œผ๋กœ ๊ณ„์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

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

์ด ๋ฌธ์ œ๋ฅผ ์œ„ ํ’€์ด์ฒ˜๋Ÿผ ์ด๋ถ„ํƒ์ƒ‰ + ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋กœ๋„ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ,

KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ํ’€ ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

์•„๋‹ˆ, ์‚ฌ์‹ค KMP๊ฐ€ ์ •ํ•ด์ธ ๊ฒƒ ๊ฐ™๋‹ค.

KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์— ๊ด€ํ•œ ์„ค๋ช…์€ ํ•ด๋‹น ๋ธ”๋กœ๊ทธ ์— ๋งค์šฐ๋งค์šฐ ์ž์„ธํžˆ ์„ค๋ช…์ด ์ž˜๋˜์–ด์žˆ์—ˆ๋‹ค.

๊ผญ ํ•œ๋ฒˆ ์ฝ์–ด๋ณด์‹œ๊ธธ.

@tgyuuAn tgyuuAn added tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ์ž‘์„ฑ ์ค‘ โฑ๏ธ labels Nov 14, 2024
@tgyuuAn tgyuuAn self-assigned this Nov 14, 2024
@tgyuuAn tgyuuAn marked this pull request as ready for review November 20, 2024 14:49
Copy link
Collaborator

@H0ngJu H0ngJu left a comment

Choose a reason for hiding this comment

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

KMP ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ํ•œ์ฐธ ํ—ค๋งธ๋„ค์š”
์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ถ„๋ฅ˜์—๋„ KMP๋ผ๊ณ  ๋˜์–ด ์žˆ๊ธด ํ•˜์ง€๋งŒ, ๊ฐœ์ธ์ ์œผ๋กœ๋Š” ์ด๋ถ„ํƒ์ƒ‰์ด ์ข€ ๋” ์ง๊ด€์ ์ธ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

KMP๋ฅผ ํ’€์–ด๋ณธ ์ ์ด ์—†์–ด์„œ ์ด๋ฒˆ์— ๋‹ค์‹œ ๊ณต๋ถ€ํ•˜๋Š” ๊ณ„๊ธฐ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค !

์ข‹์€ ๋ฌธ์ œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ท

KMP ์ฑ…๊ฐˆํ”ผ

import sys

def input() : return sys.stdin.readline().rstrip()

def calc_lps(pattern):
    n = len(pattern)
    lps = [0] * n
    length = 0
    i = 1

    while i < n:
        while length > 0 and pattern[i] != pattern[length]:
            length = lps[length - 1]
        
        if pattern[i] == pattern[length]:
            length += 1
            lps[i] = length
        i += 1
    return lps

input_txt = input()
answer = 0
n = len(input_txt)

for i in range(n):
    answer = max(answer, max(calc_lps(input_txt[i:])))

print(answer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tgyuuAn ํ•œ ์ค„๋กœ๋Š” ์†Œ๊ฐœํ•  ์ˆ˜ ์—†๋Š” ๋‚จ์ž. ๋ฆฌ๋ทฐ ์™„๋ฃŒ โœ”๏ธ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants