Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- ue5
- 다익스트라
- 구현
- 트리
- 자료구조
- BFS
- 알고리즘
- 누적 합
- 그리디 알고리즘
- 유니티
- 백트래킹
- 우선순위 큐
- XR Interaction Toolkit
- 브루트포스
- VR
- 문자열
- Team Fortress 2
- 수학
- c++
- DFS
- 다이나믹 프로그래밍
- 재귀
- 투 포인터
- 그래프
- 스택
- 시뮬레이션
- 유니온 파인드
- Unreal Engine 5
- 백준
- 정렬
Archives
- Today
- Total
1일1알
백준 17609번 회문 C++ 본문
입력받은 문자열이 팰린드롬 이면 0, 문자 하나를 제거해서 팰린드롬이면 1, 둘 다 아니면 2를 출력하는 문제이다.
while문에서 문자열의 시작과 끝을 거리를 좁혀가면서 비교하면서 시작 지점이 끝 지점보다 커졌을 때까지 계속 같으면 팰린드롬이고, 만약 다르다면 시작 지점만 하나 늘리거나 끝 지점만 하나 줄여서 다시 비교를 한다.
여기서 하나 이상이 팰린드롬이라면 유사 팰린드롬이고, 둘 다 팰린드롬이 아니라면 그냥 일반 문자열이다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
bool isPalindrome(string str, int start, int end) {
while (start <= end) {
if (str[start] != str[end])
return false;
start++;
end--;
}
return true;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
string str;
cin >> n;
while (n--) {
cin >> str;
int start = 0;
int end = str.length() - 1;
int ans = 2;
while (start <= end) {
if (str[start] != str[end]) {
if ((isPalindrome(str, start, end - 1) || isPalindrome(str, start + 1, end))) {
ans = 1;
}
break;
}
start++;
end--;
}
if (start > end) {
ans = 0;
}
cout << ans << "\n";
}
};
'알고리즘' 카테고리의 다른 글
백준 13335번 트럭 C++ (0) | 2021.11.17 |
---|---|
백준 14225번 부분수열의 합 C++ (1) | 2021.11.16 |
백준 1254번 팰린드롬 만들기 C++ (0) | 2021.11.14 |
백준 2961번 도영이가 만든 맛있는 음식 C++ (0) | 2021.11.13 |
백준 2564번 경비원 C++ (0) | 2021.11.12 |