Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Archives
Today
Total
관리 메뉴

코딩로그

[백준/BOJ/C++] 15238번 Pirates 본문

YJ/C++

[백준/BOJ/C++] 15238번 Pirates

Team DAON 2021. 11. 2. 17:37

[문제]

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 while “h” appears twice.

Write a program that reads a pirate word from the terminal and writes the letter that occurs most frequently.

It is guaranteed that only one letter is the most repeated. That is, words like “aipo” won’t appear because no single letter wins.

[입력]

The input will consist of two lines.

The first line will contain the size of the word and the second the word to process.

The word will only contain lowercase letters from a to z. The size of the word will be at most 1000 characters. No spaces or other symbols will appear.

[출력]

Print a single line with the character that appears the most and the number of occurrences.

[Source Code]

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;


int main() {
	int alph[26], max = 0, ascii = 0;
	for(int i = 0; i < 26; i++){
		alph[i] = 0;
	}

	int num;
	string input;
	cin >> num >> input;

	for(int i = 0; i < input.length(); i++){
		int temp = (int)input[i] - 97;
		alph[temp]++;
	}

	for(int i = 0; i < 26; i++){
		if(alph[i] > max){
			max = alph[i];
			ascii = i;
		}
	}

	cout << char(ascii+97) << " "<< max;
}

[결과 화면]