Algorithm๐Ÿฅ‡

10809.์•ŒํŒŒ๋ฒณ์ฐพ๊ธฐ

hae02y 2023. 10. 16. 08:25
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•ด์„œ, ๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ, ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” -1์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š์œผ๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ

๊ฐ๊ฐ์˜ ์•ŒํŒŒ๋ฒณ์— ๋Œ€ํ•ด์„œ, a๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜, b๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜, ... z๊ฐ€ ์ฒ˜์Œ ๋“ฑ์žฅํ•˜๋Š” ์œ„์น˜๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

๋งŒ์•ฝ, ์–ด๋–ค ์•ŒํŒŒ๋ฒณ์ด ๋‹จ์–ด์— ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด -1์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ์–ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 0๋ฒˆ์งธ ์œ„์น˜์ด๊ณ , ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๋Š” 1๋ฒˆ์งธ ์œ„์น˜์ด๋‹ค.

์‹œ๊ฐ„ ์ œํ•œ ๋ฉ”๋ชจ๋ฆฌ ์ œํ•œ ์ œ์ถœ ์ •๋‹ต ๋งžํžŒ ์‚ฌ๋žŒ ์ •๋‹ต ๋น„์œจ
1 ์ดˆ 256 MB 198949 106141 87461 53.126%
https://www.acmicpc.net/problem/10809

ํ’€์ด

๋งŒ์•ฝ ๋“ฑ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด -1 ๋กœ ์ดˆ๊ธฐํ™” ๋˜์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  -1์ด ์•„๋‹ˆ๋ผ๋ฉด, ๋“ฑ์žฅํ•˜๋Š” ์ฒซ๋ฒˆ์งธ๊ฐ’์„ ํ• ๋‹นํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€๋‹ค.

์ฝ”๋“œ

public class ์•ŒํŒŒ๋ฒณ์ฐพ๊ธฐ {  

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

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        int[] arr = new int['z'-'a'+1];  

        for(int i=0; i<arr.length; i++){  
            arr[i] = -1;  
        }  

        String S = br.readLine();  

        for(int i=0; i<S.length(); i++){  

            if(arr[S.charAt(i) - 'a'] != -1){  
                continue;  
            }else {  
                arr[S.charAt(i) - 'a'] = i;  
            }  
        }  
        for(int i=0; i<arr.length; i++){  
            System.out.printf("%d ", arr[i]);  
        }  
        br.close();  
    }  
}
๋ฐ˜์‘ํ˜•