문제 - 크로아티아 알파벳 (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();
found=true;
break;
}
found=false;
}
if(!found){
count++;
i++;
}
}
cout<<count<<"\n";
return 0;
}
결과
