01-08 08:57
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[BAEKJOON] ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ 16139 :: ์ธ๊ฐ„-์ปดํ“จํ„ฐ ์ƒํ˜ธ์ž‘์šฉ ๋ณธ๋ฌธ

Algorithm/Baekjoon

[BAEKJOON] ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ 16139 :: ์ธ๊ฐ„-์ปดํ“จํ„ฐ ์ƒํ˜ธ์ž‘์šฉ

miinsun 2022. 4. 22. 19:54

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

์Šน์žฌ๋Š” ์ธ๊ฐ„-์ปดํ“จํ„ฐ ์ƒํ˜ธ์ž‘์šฉ์—์„œ ์ƒ์ฒด๊ณตํ•™ ์„ค๊ณ„๋ฅผ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ํ‚ค๋ณด๋“œ ์žํŒ์ด ์‹ค์šฉ์ ์ธ์ง€ ๊ถ๊ธˆํ•ด์กŒ๋‹ค. ์ด๋ฅผ ์•Œ์•„๋ณด๊ธฐ ์œ„ํ•ด ์Šน์žฌ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒ๊ฐ์„ ํ–ˆ๋‹ค. 

'๋ฌธ์ž์—ด์—์„œ ํŠน์ • ์•ŒํŒŒ๋ฒณ์ด ๋ช‡ ๋ฒˆ ๋‚˜ํƒ€๋‚˜๋Š”์ง€ ์•Œ์•„๋ด์„œ ์ž์ฃผ ๋‚˜ํƒ€๋‚˜๋Š” ์•ŒํŒŒ๋ฒณ์ด ์ค‘์ง€๋‚˜ ๊ฒ€์ง€ ์œ„์น˜์— ์˜ค๋Š” ์•ŒํŒŒ๋ฒณ์ธ์ง€ ํ™•์ธํ•˜๋ฉด ์‹ค์šฉ์ ์ธ์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.'

์Šน์žฌ๋ฅผ ๋„์™€ ํŠน์ • ๋ฌธ์ž์—ด  S, ํŠน์ • ์•ŒํŒŒ๋ฒณ X์™€ ๋ฌธ์ž์—ด์˜ ๊ตฌ๊ฐ„ [s, e]์ด ์ฃผ์–ด์ง€๋ฉด S์˜  s๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ e๋ฒˆ์งธ ๋ฌธ์ž ์‚ฌ์ด์— X๊ฐ€ ๋ช‡ ๋ฒˆ ๋‚˜ํƒ€๋‚˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์—ฌ๋ผ.

์Šน์žฌ๋Š” ๋ฌธ์ž์—ด์˜ ๋ฌธ์ž๋Š” 0๋ฒˆ์งธ๋ถ€ํ„ฐ ์„ธ๋ฉฐ, s๋ฒˆ์งธ์™€ e๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•ด์„œ ์ƒ๊ฐํ•œ๋‹ค.
์ฃผ์˜ํ•  ์ ์€ ์Šน์žฌ๋Š” ํ˜ธ๊ธฐ์‹ฌ์ด ๋งŽ๊ธฐ์— (ํ†ต๊ณ„์ ์œผ๋กœ ํฌ๊ฒŒ ๋ฌด์˜๋ฏธํ•˜์ง€๋งŒ) ๊ฐ™์€ ๋ฌธ์ž์—ด์„ ๋‘๊ณ  ์งˆ๋ฌธ์„ q๋ฒˆ ํ•  ๊ฒƒ์ด๋‹ค.

 

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

์ž…๋ ฅ 

  • ์ฒซ ์ค„์— ๋ฌธ์ž์—ด S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
  • ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 200,000์ž ์ดํ•˜์ด๋ฉฐ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์—ˆ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์ค„์—๋Š” ์งˆ๋ฌธ์˜ ์ˆ˜ q๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, ๋ฌธ์ œ์˜ ์ˆ˜๋Š” 1≤q≤200,000์„ ๋งŒ์กฑํ•œ๋‹ค.
  • ์„ธ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ (q + 2)๋ฒˆ์งธ ์ค„์—๋Š” ์งˆ๋ฌธ์ด ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅ

  • ๊ฐ ์งˆ๋ฌธ๋งˆ๋‹ค ์ค„์„ ๊ตฌ๋ถ„ํ•ด ์ˆœ์„œ๋Œ€๋กœ ๋‹ต๋ณ€ํ•œ๋‹ค. 
  • i๋ฒˆ์งธ ์ค„์— S์˜ s๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ e๋ฒˆ์งธ ๋ฌธ์ž ์‚ฌ์ด์— X๊ฐ€ ๋‚˜ํƒ€๋‚˜๋Š” ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์„œ๋ธŒํƒœ์Šคํฌ 1 (50์ )

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 2,000์ž ์ดํ•˜, ์งˆ๋ฌธ์˜ ์ˆ˜๋Š” 2,000๊ฐœ ์ดํ•˜์ด๋‹ค.

 

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

seungjaehwang
4
a 0 5
a 0 6
a 6 10
a 7 10

 

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

0
1
2
1

 

โ€‹

๐Ÿ’ป  Main.java

  • ๋ˆ„์ ํ•ฉ์„ 2์ฐจ์› ๋ฐฐ์—ด sum์— ์ €์žฅํ•ด์ค€๋‹ค. [๋ช‡๋ฒˆ์งธ ๋ฌธ์ž์ธ์ง€][์–ด๋–ค ์•ŒํŒŒ๋ฒณ์ด ์ถ”๊ฐ€๋๋Š”์ง€]
  • s๋ถ€ํ„ฐ e๊นŒ์ง€ ๋ˆ„์ ํ•ฉ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š”
    • sum[e][์ฐพ์€ ์•ŒํŒŒ๋ฒณ ์ธ๋ฑ์Šค] - sum[s-1][์ฐพ์€ ์•ŒํŒŒ๋ฒณ ์ธ๋ฑ์Šค]
    • ๋งŒ์•ฝ ์‹œ์ž‘์ ์ด 0์ด๋ฉด ๊ทธ๋ƒฅ sum[e][์ฐพ์€ ์•ŒํŒŒ๋ฒณ ์ธ๋ฑ์Šค]์ด ๋‹ต์ด ๋œ๋‹ค.
/* ๋ฐฑ์ค€ ๋ˆ„์  ํ•ฉ - 16139 :: ์ธ๊ฐ„-์ปดํ“จํ„ฐ ์ƒํ˜ธ์ž‘์šฉ */
import java.util.*;
import java.io.*;

public class Main{
    public static void main(String[] args)throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String S = br.readLine();
        int[][] sum = new int[S.length()][26];
        
        // 1๋ฒˆ์งธ ์ดˆ๊ธฐํ™”
        sum[0][S.charAt(0) - 'a']++;
        
        // 1~๋๊นŒ์ง€ ๋ˆ„์ ํ•ฉ ๊ตฌํ•˜๊ธฐ
        for(int i = 1; i < S.length(); i++) {
        	int tmp = S.charAt(i) - 'a';

        	for(int j = 0; j < 26; j++) {
        		sum[i][j] = sum[i - 1][j];
    		}
        	sum[i][tmp]++;
        }
        
        int q = Integer.parseInt(br.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < q; i++) {
        	StringTokenizer st = new StringTokenizer(br.readLine());
        	char find = st.nextToken().charAt(0);
        	int s = Integer.parseInt(st.nextToken());
        	int e = Integer.parseInt(st.nextToken());
        	
        	if(s == 0) {
        		sb.append(sum[e][find - 'a']).append('\n');
        	}
        	else {
        		sb.append(sum[e][find - 'a'] - sum[s - 1][find - 'a']).append('\n');
        	}
        }
        
        System.out.println(sb);
    }
}
Comments