https://www.acmicpc.net/problem/14405
문제 설명
입력받은 문자열이 "pi", "ka", "chu" 이 세 개의 문자열으로 연결되어 있으면 YES 그렇지 않으면 NO를 출력한다.
주요 로직
1. 문자열의 특정 부분을 추출하기
-> substr()함수를 사용해 문자열의 2~3자리를 추출해 원하는 문자열이 존재하는지 확인한다.
정답 코드
#include <bits/stdc++.h>
using namespace std;
bool flag = true;
string str;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> str;
for(int i=0; i<str.size(); i++){
if(i+1 < str.size() && (str.substr(i,2) == "pi" || str.substr(i,2) == "ka"))
i += 1;
else if(i+2 < str.size() && str.substr(i,3) == "chu")
i += 2;
else flag = false;
}
if(flag) cout << "YES";
else cout << "NO";
}
'PS' 카테고리의 다른 글
[백준] 14469번 소가 길을 건너간 이유 3 (C++) (1) | 2024.09.27 |
---|---|
[백준] 2109번 순회 강연 (C++) (0) | 2024.09.25 |
[백준] 2234번 성곽 (C++) (0) | 2024.09.21 |
[백준] 1094번 막대 (C++) (0) | 2024.09.21 |
[백준] 19942번 다이어트 (C++) (0) | 2024.09.16 |