01-10 04:27
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ 10809 :: ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ JAVA ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ 10809 :: ์•ŒํŒŒ๋ฒณ ์ฐพ๊ธฐ JAVA

miinsun 2022. 3. 15. 16:49

 

๐Ÿ’ฌ  ๋ฌธ์ œ ์„ค๋ช…

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

 

๐Ÿ”จ  ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ 

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

 

์ถœ๋ ฅ

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

 

์˜ˆ์ œ ์ž…๋ ฅ 1)

baekjoon

 

์˜ˆ์ œ ์ถœ๋ ฅ 1)

1 0 -1 -1 2 -1 -1 -1 -1 4 3 -1 -1 7 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1

 

โ€‹

๐Ÿ’ป  Main.java

/* ๋ฐฑ์ค€ ๋ฌธ์ž์—ด ์ฒ˜๋ฆฌ - 10808 :: ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜ */
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		
		int [] answer = new int [26];
		Arrays.fill(answer, -1);

		char [] arr = s.toCharArray();
		for(int i = 0; i < arr.length; i++) {
			if(answer[(int) arr[i] - 'a'] == -1) {
				answer[(int) arr[i] - 'a'] = i;
			}
		}
		
		 for(int i : answer) System.out.print(i + " "); 
		sc.close();
	}
}
Comments