문제
알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.
입력
첫째 줄에 알파벳 대소문자로 이루어진 단어가 주어진다. 주어지는 단어의 길이는 1,000,000을 넘지 않는다.
출력
첫째 줄에 이 단어에서 가장 많이 사용된 알파벳을 대문자로 출력한다. 단, 가장 많이 사용된 알파벳이 여러 개 존재하는 경우에는 ?를 출력한다.
예제 입력 1
Mississipi
예제 출력 1
?
예제 입력 2
zZa
예제 출력 2
Z
예제 입력 3
z
예제 출력 3
Z
예제 입력 4
baaa
예제 출력 4
A
#문제 풀이 방법
1. 알파벳 대소문자로 이루어진 단어가 입력(100만 이내 길이),
가장 많이 사용된 알파벳 대문자로 출력. 여러개 있을 경우 '?'를 출력.
2. 문자열이 주어졌을 때, 각각의 알파벳 개수를 파악해야한다. 또한 대문자와 소문자 상관없이 동일하게 처리를 해야하므로 아스키코드를 사용해보자.
3. 소문자 알파벳의 아스키코드 값은 97~122이고 대문자 알파벳의 아스키 코드값은 65~90이다.
대소문자 상관없이 동일하게 처리해야하므로 소문자일 때는 97을 빼고, 대문자일 때는 65를 빼서 동일한 값으로 간주하자.
(ex. 'a'-97=0='A'-65)
4. 3번에 따라 값을 저장하고 가장 많이 입력된 알파벳을 찾는다.
#C/C++
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
int a[26]={0};//알파벳 26개
int max=0;//알파벳 최대빈도 값
int maxidx=0;//최대값 알파벳의 인덱스
cin>>s;
int len=s.size();//문자열 길이
for(int i=0;i<len;i++)
{
if(s[i]>=97)//소문자
{
a[s[i]-97]++;
if(a[s[i]-97]>max)
{
max=a[s[i]-97];
maxidx=s[i]-97;
}
}
else//대문자
{
a[s[i]-65]++;
if(a[s[i]-65]>max)
{
max=a[s[i]-65];
maxidx=s[i]-65;
}
}
}
for(int i=0;i<26;i++)
{
if(a[i]==a[maxidx]&&i!=maxidx)
{
printf("?");
return 0;
}
}
printf("%c",maxidx+65);//대문자로 출력
return 0;
}
또는
함수를 이용해 대문자를 소문자로 변경하고 처리해보는 방법이 있을 수 있다.
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
string s;
int a[26]={0};//알파벳 26개
int max=0;
int maxidx=0;
cin>>s;
std::transform(s.begin(), s.end(), s.begin(), ::tolower);//소문자로 변경
int len=s.size();//문자열 길이
for(int i=0;i<len;i++)
{
a[s[i]-97]++;
if(a[s[i]-97]>max)
{
max=a[s[i]-97];
maxidx=s[i]-97;
}
}
for(int i=0;i<26;i++)
{
if(a[i]==a[maxidx]&&i!=maxidx)
{
printf("?");
return 0;
}
}
printf("%c",maxidx+65);
return 0;
}
#Python
파이썬의 경우 입력받을 때 바로 대문자를 소문자로 변경가능하므로 이를 이용해보자.( input().lower() )
추가적으로 파이썬의 경우에는 함수들을 이용하여 좀 더 편하게 사용이 가능했다. 아래와 같이 풀 수도 있으니 참고해야겠다. (참고:49257292번 소스 코드 (acmicpc.net))
S = input().lower()
ls = [S.count(chr(x)) for x in range(ord('a'), ord('z') + 1)]
Max = max(ls)
MaxR = ls.count(Max)
if MaxR > 1:
print('?')
else:
Maxidx = ls.index(Max)
print(chr(Maxidx + ord('A')))
(cf.
→ASCII Character Codes)
Decimal | Octal | Hex | Binary | Value | Description |
000 | 000 | 00 | 0000 0000 | NUL | "null" character |
001 | 001 | 01 | 0000 0001 | SOH | start of header |
002 | 002 | 02 | 0000 0010 | STX | start of text |
003 | 003 | 03 | 0000 0011 | ETX | end of text |
004 | 004 | 04 | 0000 0100 | EOT | end of transmission |
005 | 005 | 05 | 0000 0101 | ENQ | enquiry |
006 | 006 | 06 | 0000 0110 | ACK | acknowledgment |
007 | 007 | 07 | 0000 0111 | BEL | bell |
008 | 010 | 08 | 0000 1000 | BS | backspace |
009 | 011 | 09 | 0000 1001 | HT | horizontal tab |
010 | 012 | 0A | 0000 1010 | LF | line feed |
011 | 013 | 0B | 0000 1011 | VT | vertical tab |
012 | 014 | 0C | 0000 1100 | FF | form feed |
013 | 015 | 0D | 0000 1101 | CR | carriage return |
014 | 016 | 0E | 0000 1110 | SO | shift out |
015 | 017 | 0F | 0000 1111 | SI | shift in |
016 | 020 | 10 | 0001 0000 | DLE | data link escape |
017 | 021 | 11 | 0001 0001 | DC1 | device control 1 (XON) |
018 | 022 | 12 | 0001 0010 | DC2 | device control 2 |
019 | 023 | 13 | 0001 0011 | DC3 | device control 3 (XOFF) |
020 | 024 | 14 | 0001 0100 | DC4 | device control 4 |
021 | 025 | 15 | 0001 0101 | NAK | negative acknowledgement |
022 | 026 | 16 | 0001 0110 | SYN | synchronous idle |
023 | 027 | 17 | 0001 0111 | ETB | end of transmission block |
024 | 030 | 18 | 0001 1000 | CAN | cancel |
025 | 031 | 19 | 0001 1001 | EM | end of medium |
026 | 032 | 1A | 0001 1010 | SUB | substitute |
027 | 033 | 1B | 0001 1011 | ESC | escape |
028 | 034 | 1C | 0001 1100 | FS | file separator |
029 | 035 | 1D | 0001 1101 | GS | group separator |
030 | 036 | 1E | 0001 1110 | RS | request to send/record separator |
031 | 037 | 1F | 0001 1111 | US | unit separator |
032 | 040 | 20 | 0010 0000 | SP | space |
033 | 041 | 21 | 0010 0001 | ! | exclamation mark |
034 | 042 | 22 | 0010 0010 | " | double quote |
035 | 043 | 23 | 0010 0011 | # | number sign |
036 | 044 | 24 | 0010 0100 | $ | dollar sign |
037 | 045 | 25 | 0010 0101 | % | percent |
038 | 046 | 26 | 0010 0110 | & | ampersand |
039 | 047 | 27 | 0010 0111 | ' | single quote |
040 | 050 | 28 | 0010 1000 | ( | left/opening parenthesis |
041 | 051 | 29 | 0010 1001 | ) | right/closing parenthesis |
042 | 052 | 2A | 0010 1010 | * | asterisk |
043 | 053 | 2B | 0010 1011 | + | plus |
044 | 054 | 2C | 0010 1100 | , | comma |
045 | 055 | 2D | 0010 1101 | - | minus or dash |
046 | 056 | 2E | 0010 1110 | . | dot |
047 | 057 | 2F | 0010 1111 | / | forward slash |
048 | 060 | 30 | 0011 0000 | 0 | |
049 | 061 | 31 | 0011 0001 | 1 | |
050 | 062 | 32 | 0011 0010 | 2 | |
051 | 063 | 33 | 0011 0011 | 3 | |
052 | 064 | 34 | 0011 0100 | 4 | |
053 | 065 | 35 | 0011 0101 | 5 | |
054 | 066 | 36 | 0011 0110 | 6 | |
055 | 067 | 37 | 0011 0111 | 7 | |
056 | 070 | 38 | 0011 1000 | 8 | |
057 | 071 | 39 | 0011 1001 | 9 | |
058 | 072 | 3A | 0011 1010 | : | colon |
059 | 073 | 3B | 0011 1011 | ; | semi-colon |
060 | 074 | 3C | 0011 1100 | < | less than |
061 | 075 | 3D | 0011 1101 | = | equal sign |
062 | 076 | 3E | 0011 1110 | > | greater than |
063 | 077 | 3F | 0011 1111 | ? | question mark |
064 | 100 | 40 | 0100 0000 | @ | "at" symbol |
065 | 101 | 41 | 0100 0001 | A | |
066 | 102 | 42 | 0100 0010 | B | |
067 | 103 | 43 | 0100 0011 | C | |
068 | 104 | 44 | 0100 0100 | D | |
069 | 105 | 45 | 0100 0101 | E | |
070 | 106 | 46 | 0100 0110 | F | |
071 | 107 | 47 | 0100 0111 | G | |
072 | 110 | 48 | 0100 1000 | H | |
073 | 111 | 49 | 0100 1001 | I | |
074 | 112 | 4A | 0100 1010 | J | |
075 | 113 | 4B | 0100 1011 | K | |
076 | 114 | 4C | 0100 1100 | L | |
077 | 115 | 4D | 0100 1101 | M | |
078 | 116 | 4E | 0100 1110 | N | |
079 | 117 | 4F | 0100 1111 | O | |
080 | 120 | 50 | 0101 0000 | P | |
081 | 121 | 51 | 0101 0001 | Q | |
082 | 122 | 52 | 0101 0010 | R | |
083 | 123 | 53 | 0101 0011 | S | |
084 | 124 | 54 | 0101 0100 | T | |
085 | 125 | 55 | 0101 0101 | U | |
086 | 126 | 56 | 0101 0110 | V | |
087 | 127 | 57 | 0101 0111 | W | |
088 | 130 | 58 | 0101 1000 | X | |
089 | 131 | 59 | 0101 1001 | Y | |
090 | 132 | 5A | 0101 1010 | Z | |
091 | 133 | 5B | 0101 1011 | [ | left/opening bracket |
092 | 134 | 5C | 0101 1100 | \ | back slash |
093 | 135 | 5D | 0101 1101 | ] | right/closing bracket |
094 | 136 | 5E | 0101 1110 | ^ | caret/circumflex |
095 | 137 | 5F | 0101 1111 | _ | underscore |
096 | 140 | 60 | 0110 0000 | ` | |
097 | 141 | 61 | 0110 0001 | a | |
098 | 142 | 62 | 0110 0010 | b | |
099 | 143 | 63 | 0110 0011 | c | |
100 | 144 | 64 | 0110 0100 | d | |
101 | 145 | 65 | 0110 0101 | e | |
102 | 146 | 66 | 0110 0110 | f | |
103 | 147 | 67 | 0110 0111 | g | |
104 | 150 | 68 | 0110 1000 | h | |
105 | 151 | 69 | 0110 1001 | i | |
106 | 152 | 6A | 0110 1010 | j | |
107 | 153 | 6B | 0110 1011 | k | |
108 | 154 | 6C | 0110 1100 | l | |
109 | 155 | 6D | 0110 1101 | m | |
110 | 156 | 6E | 0110 1110 | n | |
111 | 157 | 6F | 0110 1111 | o | |
112 | 160 | 70 | 0111 0000 | p | |
113 | 161 | 71 | 0111 0001 | q | |
114 | 162 | 72 | 0111 0010 | r | |
115 | 163 | 73 | 0111 0011 | s | |
116 | 164 | 74 | 0111 0100 | t | |
117 | 165 | 75 | 0111 0101 | u | |
118 | 166 | 76 | 0111 0110 | v | |
119 | 167 | 77 | 0111 0111 | w | |
120 | 170 | 78 | 0111 1000 | x | |
121 | 171 | 79 | 0111 1001 | y | |
122 | 172 | 7A | 0111 1010 | z | |
123 | 173 | 7B | 0111 1011 | { | left/opening brace |
124 | 174 | 7C | 0111 1100 | | | vertical bar |
125 | 175 | 7D | 0111 1101 | } | right/closing brace |
126 | 176 | 7E | 0111 1110 | ~ | tilde |
127 | 177 | 7F | 0111 1111 | DEL | delete |
'PS > 백준' 카테고리의 다른 글
[백준/Baekjoon]<1085번> 직사각형에서 탈출 [C/C++/Python][Class 2] (4) | 2022.09.19 |
---|---|
[백준/Baekjoon]<1546번> 평균 [C/C++/Python][Class 1] (0) | 2022.09.18 |
[백준/Baekjoon]<8958번> OX퀴즈 [C/C++/Python][Class 1] (1) | 2022.09.15 |
[백준/Baekjoon]<3052번> 나머지 [C/C++/Python][Class 1] (2) | 2022.09.13 |
[백준/Baekjoon]<2920번> 음계 [C/C++/Python][Class 1] (0) | 2022.09.12 |