๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. ๋ฌธ์ž์—ด ํญ๋ฐœ

ttoance 2022. 8. 29. 22:19
 

9935๋ฒˆ: ๋ฌธ์ž์—ด ํญ๋ฐœ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1,000,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘˜์งธ ์ค„์— ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 36๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค. ๋‘ ๋ฌธ์ž์—ด์€ ๋ชจ

www.acmicpc.net

 

๋‚ด ํ’€์ด 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.Stack;
import java.util.HashMap;
import java.util.ArrayList;

public class Main {
    
    // ๋ฌธ์ž ๋ณ„๋กœ ์œ„์น˜ ์ €์žฅ 
    static HashMap<Character, ArrayList<Integer>> characterMaps = new HashMap<Character, ArrayList<Integer>>();
    static Stack<Character> stack = new Stack<Character>();

    
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String text = br.readLine();
        String find = br.readLine();
        
        // stack์— ํ•œ ๋ฌธ์ž์—ด ์”ฉ push
        for (int i = 0; i < text.length(); i++) {
            // if(characterMaps.containsKey(text.charAt(i))){ 
            char key = text.charAt(i);
            
            stack.push(key);
            if (checkIfExists(stack, find)) {
                for (int t = 0; t < find.length(); t++) {
                      stack.pop(); 
                }
            }
        }
        
         // ๋ฐฐ์—ด string ์œผ๋กœ ๋งŒ๋“ค๊ธฐ 
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < stack.size(); i++) {
             sb.append(stack.get(i));
        }
        
        
        String answer = sb.toString();
        System.out.println((answer.length() == 0) ? "FRULA" : answer);
    }
    
    public static boolean checkIfExists(Stack<Character> stack, String find) {
        boolean result = true; 
        if (stack.size() >= find.length()) {
            int index = stack.size() - find.length();
            for (int i = 0; i < find.length(); i++) {
                if (find.charAt(i) != stack.get(index + i)) {
                    result = false; 
                    break;
                }
            }
        } 
        else {
            result = false; 
        }
        return result; 
    }
}

 

๋ฌธ์ œ๋ฅผ ์™„์ „ํžˆ ์ž˜๋ชป ์ดํ•ดํ•ด์„œ ์•„๋ž˜ ์˜ˆ์‹œ์—์„œ 1,2,a,b ๋ฅผ ๊ฐ๊ฐ ํ™•์ธํ•˜๋Š” ํ’€์ด๋กœ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ๊ณ„์† 2ํ”„๋กœ ๋Œ€์—์„œ ์‹คํŒจํ•ด์„œ ๊ฐ์žก๊ณ  ๋‹ค์‹œ ๋ณด๋‹ˆ 12ab์ž์ฒด๋ฅผ ํ™•์ธํ•ด์„œ ํญ๋ฐœ์‹œํ‚ค๋Š” ๋ฌธ์ œ์˜€๋‹ค.

12ab112ab2ab
12ab

 

 

 

์ฐธ๊ณ  : char array ๋กœ ํ‘ธ๋Š” ๋ฐฉ๋ฒ• ํ’€์ด 

 

[๋ฐฑ์ค€] 9935๋ฒˆ ๋ฌธ์ž์—ด ํญ๋ฐœ (Java)

๋ฐฑ์ค€ 9935๋ฒˆ ๋ฌธ์ž์—ด ํญ๋ฐœ : https://www.acmicpc.net/problem/9935 9935๋ฒˆ: ๋ฌธ์ž์—ด ํญ๋ฐœ ๋ฌธ์ œ ์ƒ๊ทผ์ด๋Š” ๋ฌธ์ž์—ด์— ํญ๋ฐœ ๋ฌธ์ž์—ด์„ ์‹ฌ์–ด ๋†“์•˜๋‹ค. ํญ๋ฐœ ๋ฌธ์ž์—ด์ด ํญ๋ฐœํ•˜๋ฉด ๊ทธ ๋ฌธ์ž๋Š” ๋ฌธ์ž์—ด์—์„œ ์‚ฌ๋ผ์ง€๋ฉฐ, ๋‚จ์€ ๋ฌธ

leveloper.tistory.com

 

๋ฐ˜์‘ํ˜•