smooth waters run deep

1d-1c/BOJ

2941_크로아티아 알파벳 (C++)

yeon_11 2020. 8. 26. 17:00
 

2941번: 크로아티아 알파벳

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=

www.acmicpc.net

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

int main() {
	string str;
	cin >> str;

	deque<char> str_c;
	for (int i = 0; i < str.length(); i++) {
		str_c.push_back(str[i]);
	}

	int ans = 0;
	while (!str_c.empty()) {
		char cur = str_c.front();
		char next;
		str_c.pop_front();

		if (cur == 'c') {
			next = str_c.front();
			if (next == '=') {
				str_c.pop_front();
				ans++; //c=
			}
			else if (next == '-') {
				str_c.pop_front();
				ans++; //c-
			}
			else ans++; //c
		}
		else if (cur == 'd') {
			next = str_c.front();
			if (next == '-') {
				str_c.pop_front();
				ans++; //d-
			}
			else if (next == 'z') {
				str_c.pop_front();
				char temp = str_c.front();
				if (temp == '=') {
					str_c.pop_front();
					ans++; //dz=
				}
				else {
					str_c.push_front(temp);
					ans++; //dz
				}
			}
			else ans++; //d
		}
		else if (cur == 'l') {
			next = str_c.front();
			if (next == 'j') {
				str_c.pop_front();
				ans++; //lj
			}
			else ans++; //l
		}
		else if (cur == 'n') {
			next = str_c.front();
			if (next == 'j') {
				str_c.pop_front();
				ans++; //nj
			}
			else ans++; //n
		}
		else if (cur == 's') {
			next = str_c.front();
			if (next == '=') {
				str_c.pop_front();
				ans++; //s=
			}
			else ans++; //s
		}
		else if (cur == 'z') {
			next = str_c.front();
			if (next == '=') {
				str_c.pop_front();
				ans++; //z=
			}
			else ans++; //z
		}
		else ans++;
	}
	

	cout << ans;
	return 0;
}

 

[문제 풀이 생각 과정] ----- 어어어엄청 어렵고 복잡하게 생각했다! 이렇게 푼사람은 나밖에 없을수도 헤헤

1. 입력받은 문자열을 연달아서 하나하나 확인해야겠다고 생각해서 - 큐에 넣고 하나씩 pop해서 비교하면 되겠다라고 생각했다.

2. 하지만 dz=경우에는 문자 세개를 pop해서 비교하고, 만약 dz=가 아니고 예를들면 dzo일 경우에는 마지막 o를 다시 넣어야됐다.

  그래서 front, back 모두 push,pop이 가능한 deque로 구현했다.

  1) 입력받은 문자열을 deque에 push하고

  2) 문자 하나씩 front_pop하면서 크로아티아 알파벳에 해당하는 알파벳이 나올 경우에 - 하나 더 front_pop해서 해당하는 지 확인

  3) 해당하면 해당문자들 모두 front_pop, ans++ / 아니면 문자 하나로 보기 때문에 ans++만 해준다.

 

너무 내마음대로 푼 것같아서 다른 사람 풀이 찾아보고 다시 풀었다ㅎㅎ

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

int main() {
	string str;
	cin >> str;

	vector<string> v = { "c=", "c-", "dz=", "d-", "lj", "nj", "s=", "z=" };
	
	int same = 0;
	int ans = 0;
	for (int i = 0; i<v.size(); i++) {
		while (1) {
			same = str.find(v[i]); //일치했을 때 문자열 내 위치
			if (same != string::npos) { //일치하는 게 있다면
				str.replace(same, v[i].length(), "1"); //1로 바꾸기
				ans++;
			}
			else break;
		}
	}

	for (int i = 0; i < str.length(); i++) {
		if (str[i] != '1')
			ans++;
	}
	cout << ans;
	return 0;
}

[문제 풀이 생각 과정]

1. 크로아티아 알파벳에 해당하는 문자열을 vector로 만들고, 입력받은 문자열str에 vector안에 문자열이 있는지 확인하는 방법으로 풀었다.

2. string.find(문자열)은 string 속에 '문자열'과 일치하면: string 속 '문자열'이 시작되는 위치를 리턴하고/ 일치하지 않는다면: npos를 리턴한다.

  일치하는 문자열이 있다면 - 해당 문자열을 1로 바꾸고 ans++했다.

  --- 이때! 예)c=c= 인경우 첫번째 c=의 시작하는 위치를 리턴하기 때문에 첫번째 c=만을 1로 바꾸어준다.

       그렇기 때문에 c=c=와 같이, 해당하는 문자열이 여러개 존재하는 경우를 생각해야 한다.

       while(1)을 사용하여, 해당하는 문자열이 더이상 존재하지 않을 때까지 돌려줘야 한다.

3. 입력받은 문자열에서 1이 아닌 문자를 카운트 해주고, 마지막으로 ans를 출력한다.

 

이 방법을 처음 보자마자 헉했다. 너무 똑똑하게 잘 풀어서!!

문자열의 find, replace를 처음 써봤다. 문자열 나올때마다 사용하려 노력해야겠다ㅎ__ㅎ

 

'1d-1c > BOJ' 카테고리의 다른 글

11399_ATM (JAVA)  (0) 2020.09.09
1316_그룹 단어 체커 (C++)  (0) 2020.08.28
5622_다이얼 (C++)  (0) 2020.08.26
2908_상수 (C++)  (0) 2020.08.26
1157_단어 공부 (C++)  (0) 2020.08.25