오늘의 백준 2941호 : 크로아티아(C++, 파이썬)

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

전체 코드: C++ 버전

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

int main() {
	vector<string> croatia = { "c=", "c-", "dz=", "d-", "lj", "nj","s=", "z=" };
	string input;
	cin >> input;

	for (int i = 0; i < croatia.size(); i++) {
		int s = input.find(croatia(i));
		while (input.find(croatia(i)) != string::npos) {
			input.replace(s, croatia(i).length(), "*");
            s = input.find(croatia(i), s+1);
		}
	}
	
	cout << input.length();
}

크로아티아어 알파벳을 배열 문자열로 지정할 수도 있습니다. croatia = {~~~};

string::npos 찾기 함수에 값이 없으면 인덱스가 반환되지 않습니다. 이때 존재하지 않는 문자열을 찾으라고 하면 string::npos가 반환된다.

다시 말해서,

while (input.find(croatia(i)) != string::npos) { //문자열이 발견된다면
			input.replace(s, croatia(i).length(), "*"); //해당 인덱스에서 크로아티아 알파벳 길이만큼 *로 바꾸겠다
            s = input.find(croatia(i), s+1); // s+1 인덱스부터 다시 find
		}

찾기() # 포함

구문: find(“찾을 문자열”, 검색을 시작할 인덱스);

while 문을 사용하는 이유: 동일한 크로아티아어 알파벳이 여러 개 있을 수 있으므로 모두 찾을 때까지 반복합니다.

전체 코드 : Python 버전.

croatia = ('c=", "c-', 'dz=', 'd-', 'lj', 'nj', 's=", "z=')

N = input()

for i in croatia :
    N = N.replace(i, '*')

print(len(N))

입력으로 입력된 값에서 크로아티아어 배열에 해당하는 값을 찾아 값이 있으면

텍스트로 바꿉니다.

그런 다음 문자열의 길이를 출력하십시오.