인프런 커뮤니티 질문&답변
1-K 맞왜틀?
해결된 질문
작성
·
287
0
http://boj.kr/1cea1d70b28a4da8b871d48acb7c4907
홀수가 2개 이상이면 팬린드롬이 불가하고 아니면 팰린드롬을 만드는데
알파벳 개수가 1개이면 홀수 문자에 넣고 아니면 개수의 반 만큼 word에 넣는다 이때, 홀수개이면 홀수 문자에 넣고 끝에 추가한다 그리고 뒤집은 word를 추가한다
이런 식으로 짰는데 실행하면 맞는데 왜 틀렸다고 뜨는지 궁금합니다!
답변 1
1
큰돌
지식공유자
안녕하세요 세희님 ㅎㅎ
이 경우에 oddCnt가 0일 때 에러가 발생하게 됩니다.
제가 조금 다듬어봤는데요.
#include<bits/stdc++.h>
using namespace std;
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    string input;
    string output, word, reverseWord;
    int oddCnt = 0;
    char oddChar;
    int alphabetCnt[26] = {0,};
    cin >> input;
    
    for(int i = 0; i < input.size(); i++){
        alphabetCnt[(int)(input[i]) - 65]++;
    }
    
    for(int item : alphabetCnt){
        if(item % 2 != 0){
            oddCnt++;
        }   
    } 
    if(oddCnt >= 2){  
        output = "I'm Sorry Hansoo";
    }
    else {  
        for(int i = 0; i < 26; i++){
            if(alphabetCnt[i] % 2 != 0){ // If odd count
                oddChar = (char)i + 65;
            }
            for(int j = 0; j < alphabetCnt[i] / 2; j++){
                word += (char)i + 65;
            }
        }
        output = word;
        reverse(word.begin(), word.end());
        if (oddCnt == 1) {  
            output += oddChar;
        }
        output += word;
    }
    cout << output;
}이렇게 해보시겠어요?
또 질문 있으시면 언제든지 질문 부탁드립니다.
좋은 수강평과 별점 5점은 제게 큰 힘이 됩니다. :)
감사합니다.
강사 큰돌 올림.





