Baekjoon[백준] 2941 크로아티아 알파벳 문제 C++ 풀이

less than 1 minute read

문제 링크

https://www.acmicpc.net/problem/2941


안녕하세요.

SW 알고리즘 문제로 유명한 사이트 Baekjoon Online Judge의 소스코드를 공유해드립니다.

도움이 되었으면 좋겠습니다.

감사합니다.


해답

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

string cro[8] = { "c=", "c-", "dz=", "d-", "lj", "nj", "s=" ,"z=" };

int main()
{
    int ans = 0; //답
    string str, sub;
    cin >> str;
    int n = str.size();


    for (int i = 0; i < 8; i++)
    {
	    vector<size_t> positions; 

	    size_t pos = str.find(cro[i], 0); 
	    while (pos != string::npos) 
	    {
		    positions.push_back(pos); 
		    pos = str.find(cro[i], pos + 1);
	    }

		//크로아티아 알파벳을 "0"으로 치환후 ans를 추가.
	    for (int j = 0; j < positions.size(); j++) 
	    {
		if (i != 2)
			//"c=c="와 같은 경우 포지션이 당겨지는 문제 떄문에 -j를 해줌.
			str.replace(positions[j]-j, 2, "0"); 

		else  // "dz=" 인 경우
			str.replace(positions[j]-2*j, 3, "0"); 
		
		ans++;
	}
}

//'0'을 제외
for (int i = 0; i < str.size(); i++)
{
	if (str[i] != '0')
		ans++;
}

cout << ans;

return 0;

}

Leave a comment