코딩로그
[백준/BOJ/C++] 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 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;
}
[결과 화면]
'YJ > C++' 카테고리의 다른 글
[백준/BOJ/C++] 9950번 Rectangles (0) | 2021.11.02 |
---|---|
[백준/BOJ/C++] 4575번 Refrigerator Magnets (0) | 2021.11.02 |
[백준/BOJ/C++] 4589번 Gnome Sequencing (0) | 2021.11.02 |
[백준/BOJ/C++] 13771번 Presents (0) | 2021.11.02 |
[백준/BOJ/C++] 12517번 Centauri Prime (Small1) (0) | 2021.11.02 |