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

36-Munbin-Lee #144

Merged
merged 2 commits into from
Mar 4, 2024
Merged

36-Munbin-Lee #144

merged 2 commits into from
Mar 4, 2024

Conversation

Munbin-Lee
Copy link
Member

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

https://www.acmicpc.net/problem/15927

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

30๋ถ„

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

๋ฌธ์ œ ์„ค๋ช…

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹Œ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ์ค‘ ๊ฐ€์žฅ ๊ธด ๊ฒƒ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜์ž.
๊ทธ๋Ÿฐ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์—†์œผ๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค.


๋จผ์ €, -1์„ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.

๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ ๊ฒฝ์šฐ์ด๋‹ค.

๋ชจ๋“  ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ ๊ฒฝ์šฐ๋Š”, ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ๋ฌธ์ž๊ฐ€ ํ•œ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฐ–์— ์—†๋‹ค.

A, AA, AAA, ZZZ ๋“ฑ...


์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹ˆ๋ผ๋ฉด?

๊ทธ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ผ๋ฉด?

ํ•œ ๋ฌธ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋Š” ๊ฒฝ์šฐ๋ฅผ ์œ„์—์„œ ์ด๋ฏธ ๊ณ ๋ คํ–ˆ์œผ๋ฏ€๋กœ, ๋‚จ์€ ํŒฐ๋ฆฐ๋“œ๋กฌ์€ ABBA, AB?BA ์™€ ๊ฐ™์€ ๊ผด์ด๋‹ค.

์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋นผ๋ฒ„๋ฆฐ๋‹ค๋ฉด?

๋ฌด์กฐ๊ฑด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹Œ ๋ฌธ์ž์—ด์ด ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด - 1์„ ์ถœ๋ ฅํ•˜๋ฉด ๋œ๋‹ค.


๊ณจ๋“œ ๋žœ๋ค์„ ๋Œ๋ ธ๋”๋‹ˆ ๋„ˆ๋ฌด ์‰ฌ์šด ๋ฌธ์ œ๊ฐ€ ๋‚˜์™”๋Š”๋ฐ

๋‹ค์Œ PR์€ ๋ถˆ๋‹ญ๋ง›์œผ๋กœ ๋„์ „ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

๐Ÿ“š ์ „์ฒด ์ฝ”๋“œ

s = open(0).read().rsplit()[0]

if len(set(s)) == 1:
  print(-1)
  exit()

if s == s[::-1]:
  print(len(s) - 1)
  exit()

print(len(s))

ํŒŒ์ด์ฌ์œผ๋กœ ํ•˜๋ฉด ๋”์šฑ ๊ฐ„๋‹จํ•˜๋‹ค.

Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

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

์™€ ์ฒ˜์Œ์—” ๊ทธ๋ƒฅ ํŒฐ๋ฆฐ๋“œ๋กฌ ํ’€๋“ฏ์ด DP๋กœ ๋งŒ๋“ค์–ด์„œ ์ฒดํฌํ–ˆ๋Š”๋ฐ ๋ฉ”๋ชจ๋ฆฌ ์ดˆ๊ณผ๊ฐ€ ๋‚˜๋”๋ผ๊ณ ์š”...

๊ธธ์ด๊ฐ€ ์ตœ๋Œ€ 50์ธ์ค„ ์•Œ์•˜๋Š”๋ฐ 50๋งŒ์ด์—ˆ์–ด์š”...

string = input()
N = len(string)
DP = [[False for _ in range(N)] for _ in range(N)]

for idx in range(N):
    DP[idx][idx] = True

    if idx > 0 and string[idx] == string[idx-1]:
        DP[idx-1][idx] = True

    if idx < N-1 and string[idx] == string[idx+1]:
        DP[idx][idx+1] = True

for count in range(2,N):
    for start_idx in range(N-count):
        end_idx = start_idx+count
        
        if DP[start_idx+1][end_idx-1] == True and string[start_idx] == string[end_idx]:
            DP[start_idx][end_idx] = True

answer = -1
for start_idx in range(N):
    for end_idx in range(start_idx+1,N):
        if DP[start_idx][end_idx] == False:
            answer = max(answer, end_idx-start_idx+1)

print(answer)

๋„์ €ํžˆ 50๋งŒ์„ ์–ด๋–ป๊ฒŒ $O(N)$ ์•ˆ์— ๋ฉ”๋ชจ๋ฆฌ๋ฅผ 2์ฐจ์›์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํ†ต๊ณผํ•  ์ˆ˜ ์žˆ์„๊นŒ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€,

์ง์ ‘ ์˜ˆ์ œ๋ฅผ ํ•˜๋‚˜ ์”ฉ ํŒฐ๋ฆฐ๋“œ๋กฌ ์ฒดํฌํ•˜๋ฉด์„œ ๊ทธ๋ ค๋ดค๋Š”๋ฐ ์ด๊ฒŒ ์™ ๊ฑธ...?

image




์ „์ฒด ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ด์—ˆ์„ ๊ฒฝ์šฐ์—,

๋งจ ๋งˆ์ง€๋ง‰ ํ˜น์€ ๋งจ ์•ž์˜ ๋ฌธ์ž์—ด์„ ์ œ๊ฑฐํ•œ ์ฒด๋กœ๋งŒ ๊ฒ€์‚ฌํ•˜๋ฉด ๋˜๋”๋ผ๊ณ ์š”...




๋งŒ์•ฝ ์ด ์ƒํ™ฉ์—์„œ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด ๋ชจ๋“  ๋ฌธ์ž์—ด์ด ๊ฐ™์€ ์ƒํƒœ

(์ฆ‰, ์ง์ˆ˜์™€ ํ™€์ˆ˜์ผ๋•Œ ๋ชจ๋‘ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด) ์ด๋ผ์„œ -1์„ ์ถœ๋ ฅํ•˜๊ณ ,




๋งŒ์•ฝ ๊ทธ ์ƒํ™ฉ์—์„œ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด ์•„๋‹๊ฒฝ์šฐ, ๊ทธ ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋„ค์š”....




๋Œ€๋ฐ•์ด๋„ค์š”.. ๋’คํ†ต์ˆ˜ ์–ผ์–ผํ•˜๋„ค...

string = input()

if string != string[::-1]:
    print(len(string))

elif string[1:] == string[:0:-1]:
    print(-1)

else: print(len(string)-1)

๊ทธ๋ฆฌ๊ณ  ์ด๊ฑฐ ์• ๋“œ ํ˜น์ด๋ผ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋„ ์ด ๋ฌธ์ œ ๋•์— ์ฒ˜์Œ ์•Œ์•˜๋„ค์š”..

์ธ์‚ฌ์ดํŠธ ๋„“ํ˜€๊ฐ‘๋‹ˆ๋‹ค...




์‰ฌ์šด ๋ฌธ์ œ๊ฐ€ ์ „ํ˜€ ์•„๋‹ˆ์—ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ์š”..?

Copy link
Collaborator

@pknujsp pknujsp left a comment

Choose a reason for hiding this comment

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

์•„๋‹ˆ ํŒŒ์ด์ฌ ํ’€์ด ์ฝ”๋“œ๋Ÿ‰์ด ์ฒœ์žฌ์ธ๋ฐ์š” ใ„ทใ„ทใ„ทใ„ท
์ œ ์ฝ”๋“œ๋ž‘ ๋น„๊ตํ•ด๋ณด๋‹ˆ ๋ถˆํ•„์š”ํ•œ ๋กœ์ง ๊ฑท์–ด๋‚ด์„œ ์ตœ์ ํ™”ํ•˜๋ฉด ์จ๋‘์‹  ์ฝ”๋“œ๊ฐ€ ๋˜๊ตฐ์š”

์ž…๋ ฅ ๋ฌธ์ž์—ด ์ž์ฒด๊ฐ€ ํŒฐ๋ฆฐ๋“œ๋กฌ์ด๋ฉด ์™ผ์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ๋บด๊ฐ€๋ฉด์„œ ํ•ด๋‹น ๋ฌธ์ž์˜ ๊ฐœ์ˆ˜๊ฐ€ ํ™€์ˆ˜๋ผ๋ฉด ๊ทธ๊ฑธ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€๊ธธ์ด๋กœ ์žก๊ณ  ์ถœ๋ ฅํ•˜๊ฒŒ ํ–ˆ๋Š”๋ฐ

๋‹ค์‹œ ํ™•์ธํ•ด๋ณด๋‹ˆ ์ „์ฒด ๊ธธ์ด์—์„œ 1์„ ๋นผ๋ฉด ๋˜๋Š”๊ฑฐ ์˜€๋„ค์š”

from collections import *

S = input()
string = deque()

l = 0
r = len(S) - 1
counter = defaultdict(int)

while l <= r:
    counter[S[l]] += 1
    string.append(S[l])
    
    if string[-1] == S[r]:
        if l != r:
            string.append(S[r])
            counter[S[r]] += 1
    else:
        print(len(S))
        exit()
        
    l += 1
    r -= 1

if len(counter) == 1:
    print(-1)
    exit()
    
while string:
    x = string.popleft()
    counter[x] -= 1
    if counter[x] % 2:
        print(len(string))
        exit()
        
print(-1)

@Munbin-Lee Munbin-Lee merged commit 84d26c3 into main Mar 4, 2024
@Munbin-Lee Munbin-Lee deleted the 36-Munbin-Lee branch March 4, 2024 06:41
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