문제) 백준 - 구현 - 회문
https://www.acmicpc.net/problem/17609
17609번: 회문
각 문자열이 회문인지, 유사 회문인지, 둘 모두 해당되지 않는지를 판단하여 회문이면 0, 유사 회문이면 1, 둘 모두 아니면 2를 순서대로 한 줄에 하나씩 출력한다.
www.acmicpc.net
글자의 앞과 뒤를 비교하여 회문 여부를 판단합니다. 만약 앞(l), 뒤(r) 글자가 같지 않으면 회문이 아니므로 유사 회문 판단을 진행합니다. 유사 회문 판단은 l+1번째 글자부터 r번째 글자의 회문 여부와 l 번째 글자부터 r - 1번째 글자의 회문 여부를 판단합니다. 전자는 l번째 글자를 제외할 경우의 유사 회문 판단이고 후자는 r번째 글자를 제외할 경우의 유사회문 판단입니다.
C++ 소스코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bool chkPld(string s, int l, int r){ | |
if(l >= r) return true; | |
while(l < r){ | |
if(s[l] != s[r]) | |
return false; | |
l++; r--; | |
} | |
return true; | |
} | |
int solve(string s){ | |
bool isPld = true; | |
bool isSimPld = false; | |
int l = 0, r = s.size() - 1; | |
while(l < r){ | |
if(s[l] != s[r]){ | |
isPld = false; | |
if(chkPld(s, l + 1, r)){ | |
isSimPld = true; | |
break; | |
} | |
if(chkPld(s, l, r - 1)){ | |
isSimPld = true; | |
break; | |
} | |
break; | |
} | |
l++; r--; | |
} | |
if(isPld) return 0; | |
else if(isSimPld) return 1; | |
else return 2; | |
} |
Full Code)
https://github.com/Chocochip101/BOJ_Solution/blob/main/Solution/17609_%ED%9A%8C%EB%AC%B8.cpp
GitHub - Chocochip101/BOJ_Solution: BOJ Solutions
BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.
github.com
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 22943번 - 수 (Python) 문제 및 풀이 (0) | 2022.03.02 |
---|---|
[백준] 18405번 - 경제적 전염 (C++) 문제 및 풀이 (0) | 2022.03.02 |
[백준] 20162번 - 간식 파티 (C++) 문제 및 풀이 (0) | 2022.03.01 |
[백준] 9242번 - 폭탄 해체 (C++) 문제 및 풀이 (0) | 2022.03.01 |
[백준] 12851번 - 숨바꼭질 2 (C++) 문제 및 풀이 (0) | 2022.03.01 |
댓글