PS

[백준] 14405번 피카츄 (C++)

-minari- 2024. 9. 23. 20:46

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";
}