inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

묻고 답해요

173만명의 커뮤니티!! 함께 토론해봐요.

1-K 질문드립니다!

미해결

10주완성 C++ 코딩테스트 | 알고리즘 코딩테스트

안녕하세요 강사님! 우선 강사님이 풀이하신 코드는 참고하였고, 제가 한 방식보다는 훨씬 효율적이고 이해하기 쉬운 코드였습니다. 다만, 제 코드는 왜 안되는지 이해는 하고 넘어가고 싶어서 질문 남깁니다ㅠㅠ 반례체크 하였고 타 온라인 프로그램에서 실행했을때 잘 나오는데 어디가 잘못 된건지 찾지를 못하겠어서요.. 알려주시면 정말 감사하겠습니다ㅠㅠ!! using namespace std; #include <iostream> #include <map> string name; int front, back, count = 0; char mid; map<char,int> alpha; bool possible = true; int main() { cin >> name; string ret = ""; char palindrome[name.length()]; map<char,int>::iterator it; for(char c : name) { alpha[c]++; } // 이름길이가 짝수 일 경우 if(name.length() % 2 == 0) { it = alpha.begin(); while(it != alpha.end()) { // 홀수 존재하면 안됌 if(it->second % 2 != 0) { possible = false; } it++; } if(possible) { front = 0, back = name.length()-1; for(it = alpha.begin(); it != alpha.end(); it++) { while(it->second > 0) { palindrome[front] = it->first; palindrome[back] = it->first; front++, back--, it->second -= 2; } } } } // 이름길이가 홀수 일 경우 else { it = alpha.begin(); while(it != alpha.end()) { if(it->second % 2 != 0) { // 홀수 한개만 존재 가능 if(count > 1) { possible = false; } else { mid = it->first; it->second--; count++; } } it++; } if(possible) { front = 0, back = name.length()-1; for(it = alpha.begin(); it != alpha.end(); it++) { while(it->second > 0) { palindrome[front] = it->first; palindrome[back] = it->first; front++, back--, it->second -= 2; } } palindrome[name.length()/2] = mid; } } if(possible) { for(int i = 0; i < name.length()-1; i++) { ret += palindrome[i]; } cout << ret << "\n"; } else { cout << "I'm Sorry Hansoo" << "\n"; } return 0; }

  • 틀린이유
  • 코테 준비 같이 해요!
  • C++
soobinm10 댓글 1 좋아요 0 조회수 185

인기 태그

인프런 TOP Writers

주간 인기글