본문 바로가기
알고리즘/백준

1157번 단어공부[미해결]

by 헤르쯔44 2022. 9. 20.
728x90
반응형

 

 

풀이

 

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.IOException; 

public class Main {

	public static char[] alphabe;
	public static void main(String[] args) throws IOException {

		

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String word=bf.readLine();
		alphabe=new char[word.split("").length];
		//비교를 위한 현재 단어 저장 변수
		char current=word.charAt(0);
		//위치 
		int position=0;
		//처음 단어 접속
		alphabe[position]=current;
	
		for(int i=0;i<alphabe.length;i++)
		{
			char next=word.charAt(i);
		     if(current!=next&& Character.toUpperCase(current)!=next)
		     {
		    	 boolean check=true;
		    	//배열에서 기존에 next값이 들어있는지 체크
		    	 for(int j=0;j<alphabe.length;j++)
		 		{
		    		 if(alphabe[j]==next || alphabe[j]==Character.toUpperCase(next))
		    		 {
		    			 check=true;
		    			 break;
		    		 }else {
		    			 check=false;
		    		 }
		    		 
		    		 
		 		}
		    	 if(check!=true)
		    	 {
		    		 position++;
			    	 alphabe[position]=next;
			    	 current=next; 
		    	 }
		    	 
		  
		     }
		}
		
		checkCount(word);
	}
	
	public static void checkCount(String a)
	{
		//비교할 count
		int currentCount=0;
		//제일 빈도수가 많은 카운드
		int TopCount=0;
		//빈도수가 많은 알파벳
		char TopAlphabe=' ';
		for(int i=0;i<alphabe.length;i++)
		{
			 for(int j=0;j<alphabe.length;j++)
			 {
				 //대문자나 소문자랑 같으면..
				 if(alphabe[i]==a.charAt(j) || Character.toUpperCase(alphabe[i])==a.charAt(j))
				 {
					 currentCount++;
				 }
			 }
			 //카운터랑 맥스카운터비교해서 지금 카운터가 크면 값을 넣어준다.
			 if(currentCount>TopCount)
			 {
				 TopCount=currentCount;
				 TopAlphabe=alphabe[i];

			 }else if(currentCount==TopCount)
			 {
				System.out.println("?"); 
				return;
			 }
			 
			 currentCount=0;
		}
		System.out.println(TopAlphabe);
		
	}
	

	
}

 

공포의 시간초과..ㅜㅜ 이클립스에서 테스트 할때는 잘되는데 ㅜㅜㅜ

 

감을 못잡겠어서 풀이를 찾아봤다.

 

https://st-lab.tistory.com/64

 

[백준] 1157번 : 단어 공부 - JAVA [자바]

https://www.acmicpc.net/problem/1157 1157번: 단어 공부 알파벳 대소문자로 된 단어가 주어지면, 이 단어에서 가장 많이 사용된 알파벳이 무엇인지 알아내는 프로그램을 작성하시오. 단, 대문자와 소문자

st-lab.tistory.com

 

보니까 ASKII코드를 사용해서 푸는것같았다. 왜 그걸 생각을 못했는지..

 

참고해서 푼 풀이

import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.io.IOException; 

public class Main {


	public static void main(String[] args) throws IOException {

		

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String word=bf.readLine();
		int max=0;
		int topAlphabe='?';
		//알파벳 개수 만큼 배열 길이 설정
	    int[] alphabe=new int[26];
		if(word.length()>1)
		{
			 //입력된 단어길이 만큼 반복해서 검사
			for(int i=0;i<word.length();i++)
			{
				if(word.charAt(i)<'a')
				{
					alphabe[word.charAt(i)-'A']++;
				}else {
					alphabe[word.charAt(i)-'a']++;
				}


			}

			for(int i=0;i<alphabe.length;i++)
			{
			
				if(alphabe[i]>max)
				{
					max=alphabe[i];
					topAlphabe=i+65;
				}else if(alphabe[i]==max)
				{
					topAlphabe='?';
				
				}

			}	
		}else
		{
			topAlphabe=word.charAt(0);
		}
		System.out.println((char)topAlphabe);

	}


	
}

 

100%까지 채점되서 맞았나?? 했는데 틀림 ㅋㅋ...ㅜㅜㅜ

어디가 틀렸는지 모르겠어서 일단 접어두고 내일 다시보려고 한다.

내일도 모르겠으면 백준게시판에다가 물어봐야겠다.

728x90
반응형

'알고리즘 > 백준' 카테고리의 다른 글

10809번 알파벳 찾기  (1) 2022.09.22
1224[실버4] 스위치 켜고 끄기  (0) 2022.09.16
[백준] 9498번 시험 성적 문제 -java11  (0) 2021.09.14