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

메르센고수·2023년 8월 9일
0

Baekjoon

목록 보기
10/48
post-thumbnail

문제 - 크로아티아 알파벳 (Silver 5)

풀이 전략

  • 배열에 크로아티아 알파벳을 저장하고, 입력한 문자열과 배열의 원소들을 비교하면서 일치하면 count를 늘리고 알파벳 길이 만큼 index를 증가시킨다. 배열에 없는 문자가 있으면 count를 증가시키는데 index는 1만큼 증가시킨다.
  • substr 함수 사용

소스 코드

#include <iostream>
using namespace std;

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    string alpha[8]={"c=","c-","dz=","d-","lj","nj","s=","z="};
    string str;
    cin>>str;

    int count=0;
    for(int i=0;i<str.length();){
        bool found=false;
        for(string a:alpha){
            if(str.substr(i,a.length())==a){
                count++;
                i+=a.length(); // lj처럼 2글자 알파벳도 있기 때문에
                found=true;
                break;
            }
            found=false;
        }
        if(!found){
            count++;
            i++;
        } // alpha 배열에 없는 알파벳인 경우
    }
    cout<<count<<"\n";
    return 0;
}

결과

profile
블로그 이전했습니다 (https://phj6724.tistory.com/)

0개의 댓글