2023년3월28일 단계별 풀어보기로 백준 2941번 크로아티아 알파벳을 풀어보았다.
링크텍스트
여기 문제에서 나왔듯이 "c=","c-","dz=","d-","lj","nj","s=","z=" 의 크로아티안 알파벳이 들어오면 하나로 숫자를 세는 알고리즘을 작성하는 문제이다. 나는 이번 롤백에서 새로 배운 string 클래스를 사용하여 문제를 풀어보았고, if-else 문을 사용하여 풀었다.
#include <iostream>
#include<string>
using namespace std;
int main() {
string str;
cin>>str;
int cnt = 0;
int i = 0;
//"c=","c-","dz=","d-","lj","nj","s=","z="
while (i < str.length()) {
if (str[i] == 'c') {
if (str[i + 1] == '=')
i++;
else if (str[i + 1] == '-')
i++;
}
else if (str[i] == 'd') {
if (str[i + 1] == '-')
i++;
else if (str[i + 1] == 'z'&&str[i + 2] == '=')
i += 2;
}
else if (str[i] == 'l') {
if (str[i + 1] == 'j')
i++;
}
else if (str[i] == 'n') {
if (str[i + 1] == 'j')
i++;
}
else if (str[i] == 's') {
if (str[i + 1] == '=')
i++;
}
else if (str[i] == 'z') {
if (str[i + 1] == '=')
i++;
}
cnt++;
i++;
}
cout<<cnt;
}
일단 다른 블로그들도 보았는데, 백터,string멤버 함수들을 사용하여 코드를 더 짧게 작성하였다. 벡터와 string 클래스를 더 공부해야겠다.