※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 15238번 문제인 Pirates이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/15238
15238번: Pirates
Pirates talk a funny way. They say word where the letters are repeated more than they need to be. We would like know which letter appears the most frequently in a Pirate word. For example: In the word “arrrrrghh”, the letter “r” appears 5 times whi
www.acmicpc.net
주어지는 문자열의 각 문자의 개수를 세는 배열을 만들어 문제를 해결하자.
문제 조건에 따라 가장 많은 개수가 있는 문자는 유일하게 주어지므로 안심하고 구현하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int cnt[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int slen; string s; cin >> slen >> s;
for (auto l : s) cnt[l]++;
int mx = 0; char ans;
for (char i = 'a'; i <= 'z'; i++) {
if (cnt[i] > mx) {
mx = cnt[i];
ans = i;
}
}
cout << ans << ' ' << mx;
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 15477 // C++] 水ようかん (Mizuyokan) (0) | 2022.11.24 |
---|---|
[BOJ 25400 // C++] 제자리 (0) | 2022.11.24 |
[BOJ 15234 // C++] Number Pairs (0) | 2022.11.24 |
[BOJ 16189 // C++] Repetitive Palindrome (0) | 2022.11.24 |
[BOJ 25756 // C++] 방어율 무시 계산하기 (0) | 2022.11.24 |