본문 바로가기

IT/알고리즘

백준 7단계(10809)

반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
public class Main {
 
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        //스트링으로 입력받고
        String str = br.readLine();
        //int array의 default 값은 0이지만 -1로 26개의 값을 미리 선언. 알파벳은 26개가 있으니까
        int[] arr = {-1-1-1-1-1-1-1-1-1-1
                -1-1-1-1-1-1-1-1-1-1
                -1-1-1-1-1-1};
        //입력받은 str 길이만큼 실행
        for(int i = 0; i < str.length(); i++) {
            //a의 ascii 코드는 97, 만약 baekjoon이라고 예시를 들면
            //baekjoon의 첫번째 b는 alphabet의 2번째 순서이니 1번째방에 값을 입력해야된다
            //b - 97 = 1, 왜냐면 b는 98이여서, 여기서 'a'를해도 상관은 없다
            //baekjoon에서 o가 2개이다. 먼저 온 o의 순서를 저장해야되서 -1이면 값을 넣고 
            //만약 -1이 아니란 뜻은 벌써 값이 들어갔다는 뜻이여서 6번째 o는 입력이 안된다.
            if(arr[str.charAt(i) - 97== -1) arr[str.charAt(i) - 'a'= i;
        }
        //write으로 뚝딱
        for(int i = 0; i < arr.length; i++) {
            bw.write(arr[i] + " ");
        }
        bw.flush();
    }
}
 
    
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 
반응형

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

백준 7단계(1157)  (0) 2019.12.10
백준 7단계(2675)  (0) 2019.12.07
백준 7단계(11720)  (0) 2019.12.07
백준 7단계(11654)  (0) 2019.12.07
백준 6단계(1065)  (0) 2019.12.07