※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※

 

이번에 볼 문제는 백준 26536번 문제인 Cowspeak이다.
문제는 아래 링크를 확인하자.

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

 

26536번: Cowspeak

The animals are conspiring again. The cows are mooing very strangely and you’re certain of it. Your theory is that the length of each “MM” sound and the length of the “OO” sound can be translated to hexadecimal values and then converted to ascii.

www.acmicpc.net

각 주어지는 문자열의 M의 개수와 O의 개수를 세어 "(M의 개수)*16 + (O의 개수)"를 아스키 코드로 갖는 문자를 출력하는 것을 반복하는 것으로 문제를 해결하자.

 

각 줄의 마지막에 공백을 하나 추가하고, 공백이 등장할 때마다 위의 작업을 반복하게끔 구현하면 문제를 편하게 해결할 수 있다.

 

아래는 제출한 소스코드이다.

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

int T;
string s;
int cnt[128];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	cin >> T;
	getline(cin, s);
	while (T--) {
		getline(cin, s);
		s += " ";
		for (auto& l : s) {
			if (l == ' ') {
				char x = cnt['M'] * 16 + cnt['O'];
				cout << x;
				cnt['M'] = cnt['O'] = 0;
			}
			else cnt[l]++;
		}

		cout << '\n';
	}
}
728x90

'BOJ' 카테고리의 다른 글

[BOJ 10187 // C++] Golden  (0) 2022.12.21
[BOJ 26502 // C++] Decoder  (0) 2022.12.21
[BOJ 26560 // C++] Period  (0) 2022.12.21
[BOJ 26547 // C++] Square  (0) 2022.12.21
[BOJ 26510 // C++] V for Vendetta  (0) 2022.12.21

+ Recent posts