상세 컨텐츠

본문 제목

1157 : 단어 공부

프로그래밍/백준

by whave 2022. 1. 6. 12:24

본문

#include<stdio.h>
#include<string.h>
int main(void){
	char s[1000010];
	int count[30]={0};
	int max,index,i,len,equal;
	
	scanf("%s",s);
	i=0;
	len=0;
	while(s[i]!='\0'){
		i++;
		len++;
	}

	max=0;
	for(int i=0;i<len;i++){
		if(s[i]>=65&&s[i]<=90)
			count[s[i]-65]++; 
		else 
			count[s[i]-97]++;	
	}
	
	for(int i=0;i<26;i++){
		if(count[i]>max){
			max=count[i];
			index=i;
			equal=0;
		}
		else if(count[i]!=0&&count[i]==max)
			equal=1;
	}
	if(equal)
		printf("?");
	else
		printf("%c",index+65);
	return 0;
}

배열 s의 길이를 strlen함수로 구하는 코드로 제출했을 때 시간초과가 됐다.

strlen의 시간 복잡도는 O(n)이기 때문

'프로그래밍 > 백준' 카테고리의 다른 글

[C언어/DFS] 2468 : 안전 영역  (0) 2022.01.09
[C언어/DP] 10844 : 쉬운 계단 수  (0) 2022.01.09
[C언어/DFS] 4963 : 섬의 개수  (0) 2022.01.05
[C언어/DFS] 2667 : 단지번호붙이기  (0) 2022.01.04
2606 : 바이러스  (0) 2022.01.04

관련글 더보기