본문 바로가기
  • Let's study
PS/백준

[C++] 백준 2941번: 크로아티아 알파벳

by 코딩고수이고파 2020. 12. 8.

1. 크로아티아 알파벳을 배열로 저장해준다.

2. string으로 문자열을 입력받고 첫 글자가 크로아티아의 알파벳과 동일하다면 다음 글자도 비교한다.

이 때 크로아티아 알파벳이 맞다면 i++를 해주며 넘긴다. (세글자인 "dz="는 i+=2를 해준다.)

3. for문을 한 번 돌 때마다 알파벳 수를 세는 cnt를 +1 해준다. (한 글자짜리 알파벳이어도 cnt++가 됨.)

#include<iostream>
#include<string>
using namespace std;

int main() {
	string str[] = {"c=","c-","dz=","d-","lj","nj","s=","z="};
	string arr;
	cin >> arr;
	int cnt = 0;
	for (int i = 0; i < arr.size(); i++) {
		if (arr[i] == 'c') {
			if (arr[i + 1] == '=' || arr[i + 1] == '-')
				i += 1;
		}
		else if (arr[i] == 'd') {
			if (arr[i + 1] == '-')
				i++;
			else if (arr[i + 1] == 'z' && arr[i + 2] == '=')
				i += 2;
		}
		else if (arr[i] == 'l' || arr[i] == 'n') {
			if (arr[i + 1] == 'j')
				i++;
		}
		else if (arr[i] == 's' || arr[i] == 'z') {
			if (arr[i + 1] == '=')
				i++;
		}
		cnt++;
	}
	cout << cnt;
	return 0;
}

'PS > 백준' 카테고리의 다른 글

[C++] 백준 2798번: 블랙잭  (0) 2020.12.23
[C++] 백준 1316번: 그룹 단어 체커  (0) 2020.12.08
[C++] 백준 2667번: 단지번호붙이기  (0) 2020.11.29
[C++] 백준 2178번: 미로 탐색  (0) 2020.11.29
[C++] 백준 1697번: 숨바꼭질  (0) 2020.11.29

댓글