본문 바로가기
개발 노트/백준, 프로그래머스 풀이

[백준 1157/c++] 단어 공부 - 문자열

by tokkiC 2022. 6. 24.

간단한 문자열 문제다

더 효율적인 로직도 중요하지만 더 더 더 빨리 풀도록 하자 시간내에 못풀면 쉬워도 틀린문제니까

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

 

1157번: 단어 공부

알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자를 구분하지 않는다.

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;


int main(){
	
	string s;
	int a[150]={0};
	int max=0;
	int cnt=0;
	char ans='*';
	cin >> s;
	
	for(int i=0; i<s.length(); i++){
		if(s[i]>='a' && s[i]<='z')
			a[(int)s[i]-32]++;
		else {
			a[(int)s[i]]++;	
		}
	}
	
	for(int i=0; i<150; i++){
		if(max<a[i]){
			max=a[i];
		}
	}
	
	for(int i=0; i<150; i++){
		if(max==a[i]){
			cnt++;
			ans=(char)i;	
		}
	}
	
	if(cnt==1){
		cout << ans << "\n";
	} else if(cnt>1){
		cout << "?" << "\n";
	}
	
	return 0;
}

댓글