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

7-g0rnn #31

Merged
merged 2 commits into from
Nov 29, 2024
Merged

7-g0rnn #31

merged 2 commits into from
Nov 29, 2024

Conversation

g0rnn
Copy link
Collaborator

@g0rnn g0rnn commented Nov 24, 2024

πŸ”— 문제 링크

ν¬λ¦¬μŠ€λ§ˆμŠ€κ°€ ν•œλ‹¬ 정도 남은 μ‹œμ μ— λ‹¬μ½€ν•œ 문제λ₯Ό μ€€λΉ„ν–ˆμ”λ‹ˆλ‹€ 😍
νŒ°λ¦°λ“œλ‘¬ λ§Œλ“€κΈ°

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

30m

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

문제의 λ‚œμ΄λ„λŠ” 어렡지 μ•ŠμŠ΅λ‹ˆλ‹€. μ΄μ „μ²˜λŸΌ κ΅¬ν˜„μ„ λΉ λ₯΄κ³  μ •ν™•ν•˜κ²Œ ν•˜κΈ° μœ„ν•΄ λΉ„μŠ·ν•œ λ‚œμ΄λ„μ˜ 문제λ₯Ό κ°€μ Έμ™”μŠ΅λ‹ˆλ‹€.

문제 μžμ²΄κ°€ 어렡지 μ•ŠμœΌλ‹ˆ ν•œλ²ˆμ— λ§žμΆ”λŠ” 것을 μ—°μŠ΅ν•΄λ³΄μ„Έμš”..! 디버깅도 μ΅œλŒ€ν•œ 늦게 ν•˜λ©΄μ„œ 머리둜 λ§Žμ€ μ˜ˆμ™Έ 사항듀을 μƒκ°ν•˜λŠ” μ—°μŠ΅μ„ ν•˜κ³  μžˆλŠ”λ° κ½€λ‚˜ μ˜λ―ΈμžˆλŠ” 것 κ°™μ•„μš”.

❗ 아이디어 : μ•ŒνŒŒλ²³μ„ λ‹΄λŠ” 배열을 λ§Œλ“€κ³  ν•˜λ‚˜μ”© κΊΌλ‚΄μ„œ μ“΄λ‹€


0. μ•ŒνŒŒλ²³ 배열을 μ΄ˆκΈ°ν™”ν•œλ‹€.
1. alpha[]λ₯Ό μˆœνšŒν•˜λ©° valueκ°€ 1μ΄ν•˜μΌ λ•ŒκΉŒμ§€ 
    μ‚¬μš©ν•˜μ—¬ νŒ°λ¦°λ“œλ‘¬μ˜ μ ˆλ°˜μ„ λ§Œλ“ λ‹€.
3. μ ˆλ°˜μ„ ν† λŒ€λ‘œ 전체λ₯Ό 좜λ ₯ν•œλ‹€.

//μ˜ˆμ™Έ
1. μ•ŒνŒŒλ²³μ΄ ν™€μˆ˜μΈ κ²½μš°κ°€ 2개 이상인 경우 νŒ°λ¦°λ“œλ‘¬μ„ λ§Œλ“€ 수 μ—†λ‹€.

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

Copy link
Collaborator

@kangrae-jo kangrae-jo left a comment

Choose a reason for hiding this comment

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

λ§μ”€ν•˜μ‹ λŒ€λ‘œ 쑰금 μ‰¬μš΄ 편인건 λ§žλŠ”κ²ƒ κ°™λ„€μš”

ν•œλ²ˆλ§Œμ— λ§žμΆ”λ €κ³  ν–ˆλŠ”λ°... μ•„μ£Ό μ‚¬μ†Œν•œ 문제둜 1νŠΈλ§Œμ— 성곡은 μ‹€νŒ¨ν–ˆλ„€μš”
뒀에 λ‚˜μ˜€λŠ” 제 μ½”λ“œ 보면 μ•„μ‹œκ² μ§€λ§Œ,

I'm Sorry Hansooλ₯Ό 좜λ ₯ν•˜κ³ λ‚˜μ„œ
return 0; 을 μ•ˆν•˜κ³  break;λ₯Ό ν•΄λ²„λ €μ„œ ν‹€λ Έμ—ˆλ„€μš”..
(저도 30뢄정도 걸린것 κ°™μŠ΅λ‹ˆλ‹€)

그리고

c - 'A' 
c + 'A' 

이런 ν‘œν˜„μ— μ˜›λ‚ μ—λŠ” 쑰금 λΆ€μžμ—°μŠ€λŸ½λ‹€κ³  μƒκ°ν–ˆκ³ ,
μ‹€μ „μ—μ„œ μ‚¬μš©ν•˜κΈ° μ–΄λ €μ› λŠ”λ° 이제 잘 μ“°κ²Œλœ 것 κ°™μ•„μ„œ 기뢄이 μ’‹μŠ΅λ‹ˆλ‹€.

제 μ½”λ“œμ™€ κ· ν˜Έλ‹˜ μ½”λ“œ μ°¨μ΄λŠ”

1. ν™€μˆ˜ 처리
2. 좜λ ₯ λ•Œ λ‚˜λ¨Έμ§€ μ ˆλ°˜μ„ 좜λ ₯ν•˜λŠ” 방식

이정도인 λ“―ν•˜κ³  전체적인 κ΅¬μ‘°λŠ” λΉ„μŠ·ν•˜λ„€μš”
κ³ μƒν•˜μ…¨μŠ΅λ‹ˆλ‹€.

CPP CODE
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int flag[26] = { 0, };

int main() {
    string str;
    cin >> str;

    for (char c : str) {
        flag[c - 'A']++;
    }

    string result;
    char odd = '\0';
    for (int i = 0; i < 26; i++) {
        if (flag[i] % 2 == 1) {
            if (odd != '\0') {
                cout << "I'm Sorry Hansoo";
                return 0;
            } 
            else odd = i + 'A';
        }

        int len = flag[i] / 2;
        while (len--) {
            result += i + 'A';
        }
    }

    cout << result;
    if (odd != '\0') cout << odd;
    reverse(result.begin(), result.end());
    cout << result;

    return 0;
}

Comment on lines +42 to +46
while (alpha[i] > 1)
{
ret = ret + (char)(i + 'A');
alpha[i] -= 2;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

μ΄λ ‡κ²Œ 2κ°œμ”© λΉΌλŠ” 방법이 μžˆμ—ˆκ΅°μš”
μ €λŠ” μ ˆλ°˜κΉŒμ§€λ§Œ λΉΌλŠ” 방으둜 ν–ˆλŠ”λ° λ‹€μ–‘ν•œ 방법을 μ•Œκ²Œλ˜μ–΄μ„œ μ’‹λ„€μš”

@kokeunho
Copy link
Collaborator

μ €λŠ” ν’€μ΄λŠ” λ– μ˜¬λ ΈλŠ”λ° μžλ°” μˆ™λ ¨λ„ 이슈둜 κ²€μƒ‰ν•΄κ°€λ©΄μ„œ ν’€μ—ˆμŠ΅λ‹ˆλ‹€.
.toCharArray()을 μ‚¬μš©ν•΄μ„œ λ¬Έμžμ—΄μ„ 단어 ν•˜λ‚˜μ”©μœΌλ‘œ λΆ„λ¦¬ν•΄μ„œ 배열에 λ„£λŠ”λ‹€λ˜μ§€
κ΅¬ν˜„μ— μžˆμ–΄ μ—¬λŸ¬ 개 배울 수 μžˆμ—ˆλ˜ μ‹œκ°„μ΄μ—ˆμŠ΅λ‹ˆλ‹€..
μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€

Copy link
Collaborator

@wnsmir wnsmir left a comment

Choose a reason for hiding this comment

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

from collections import Counter

def make_palindrome(name):
    # Step 1: 문자 λΉˆλ„ 계산
    # μž…λ ₯된 λ¬Έμžμ—΄μ—μ„œ 각 λ¬Έμžκ°€ λͺ‡ 번 λ‚˜μ˜€λŠ”μ§€ μ„Έμ–΄μ€Œ
    counter = Counter(name)

    # Step 2: ν™€μˆ˜ 개 문자λ₯Ό μ°ΎκΈ°
    # ν™€μˆ˜ 개수의 문자λ₯Ό μ €μž₯ν•  리슀트 생성
    odd_chars = []
    for char, count in counter.items():
        if count % 2 == 1:  # ν™€μˆ˜ 개수인 경우
            odd_chars.append(char)

    # Step 3: ν™€μˆ˜ 개 λ¬Έμžκ°€ 2개 이상이면 νŒ°λ¦°λ“œλ‘¬ 생성 λΆˆκ°€
    if len(odd_chars) > 1:
        return "I'm Sorry Hansoo"

    # Step 4: νŒ°λ¦°λ“œλ‘¬μ˜ 절반 λ§Œλ“€κΈ°
    # μ ˆλ°˜μ„ μ €μž₯ν•  λ¬Έμžμ—΄ μ΄ˆκΈ°ν™”
    half = ""
    for char in sorted(counter.keys()):  # 문자λ₯Ό μ‚¬μ „μˆœμœΌλ‘œ μ •λ ¬
        half += char * (counter[char] // 2)  # ν•΄λ‹Ή 문자의 절반 개수만큼 μΆ”κ°€

    # Step 5: 쀑앙에 놓을 문자 κ²°μ •
    # ν™€μˆ˜ 개 λ¬Έμžκ°€ μžˆλ‹€λ©΄ κ·Έ 쀑 ν•˜λ‚˜λ₯Ό 쀑앙에 λ†“μŒ
    middle = odd_chars[0] if odd_chars else ""

    # Step 6: νŒ°λ¦°λ“œλ‘¬ μ™„μ„±
    # 절반 + 쀑앙 문자 + 절반(μ—­μˆœ)
    result = half + middle + half[::-1]
    return result

# μž…λ ₯ λ°›κΈ°
name = input("이름을 μž…λ ₯ν•˜μ„Έμš”: ").strip()
print(make_palindrome(name))

일단 문제λ₯Ό 처음보고 νŽ λ¦°λ“œλ‘¬μ΄ 뭔지 λͺ°λΌμ„œ μ°Ύμ•„λ΄€μŠ΅λ‹ˆλ‹€ γ…Ž,,

쀑간을 κΈ°μ€€μœΌλ‘œ μ–‘μͺ½μ΄ λŒ€μΉ­μ΄μ–΄μ•Ό ν•˜κΈ°λ•Œλ¬Έμ— ν™€μˆ˜μ˜ μ•ŒνŒŒλ²³μ€ 쀑간 ν•˜λ‚˜λ§Œ μžˆμ–΄μ•Όν•œλ‹€λŠ”κ²Œ 핡심 λ‘œμ§μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

μΉ΄μš΄ν„°λ₯Ό μ‚¬μš©ν•΄ μ•ŒνŒŒλ²³λ“€μ˜ 수λ₯Ό λ”•μ…”λŸ¬λ‹ˆλ‘œ μ €μž₯ν•΄μ£Όκ³ , ODD즉 ν™€μˆ˜λ¬Έμžλ₯Ό μ§€μ •ν•΄μ€λ‹ˆλ‹€.
μ—¬κΈ°μ„œ ν™€μˆ˜κ°€ ν•˜λ‚˜μ—¬μ•Όλ§Œ λ§Œλ“€ 수 있고 μ•„λ‹ˆλ©΄ λ¬Έμžμ—΄μ„ 좜λ ₯ν•©λ‹ˆλ‹€.

half에 짝수인 λ¬Έμžμ—΄λ“€μ„ μ‚¬μ „μˆœμœΌλ‘œ μ •λ ¬ν•΄μ„œ μΆ”κ°€ν•΄μ£Όκ³  μ€‘κ°„κΈ€μžλ₯Ό 넣은 λ’€ λ°˜λŒ€λ‘œ λ‹€μ‹œ μ ˆλ°˜μ„ λ„£μ–΄μ€λ‹ˆλ‹€.

@g0rnn g0rnn merged commit fe4e907 into main Nov 29, 2024
1 check passed
@g0rnn g0rnn deleted the 7-g0rnn branch November 29, 2024 08:19
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