유형 : 링크드 리스트

 

# 처음에는 입력받아서 arrayList를 사용하고 index를 조절하며 switch case 문으로 해결하려 했으나 시간초과로 문제를 풀 수 없었다. 다른 방법을 찾아보던 중 스택으로 해결할 수 있다는 글을 보고 스택으로 수정하여 풀었더니 통과하였다. 시간 제한을 신경써야 함.

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.StringTokenizer;

 

public class Main {

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

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String str =br.readLine();

        

        ArrayList<Character> arrList = new ArrayList<>();

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

            arrList.add(str.charAt(i));

        }

        

        int N = Integer.parseInt(br.readLine());

        

        int index = arrList.size();

        for(int i=0; i<N; i++) {

            StringTokenizer st = new StringTokenizer(br.readLine());

            char mod = st.nextToken().charAt(0);

            switch (mod) {

            case 'L':

                if(index==0)

                    break;

                else {

                    index-=1;

                    break;

                    }

            case 'D':

                if(arrList.size()==0 || arrList.size()==index)

                    break;

                else {

                    index+=1;

                    break;

                }

            case 'B':

                if(index==0 || arrList.size()==0)

                    break;

                else {

                    arrList.remove(index-1);

                    index--;

                    break;

                }

                

                

            case 'P':

                char c = st.nextToken().charAt(0);

                arrList.add(index,c);

                index++;

                break;

            }

//            for(int q=0; q<arrList.size(); q++) {

//                System.out.print(arrList.get(q));

////            }System.out.println();

        }

            for(int q=0; q<arrList.size(); q++) {

                System.out.print(arrList.get(q));

            }System.out.println();

        

    }

}

 

Colored by Color Scripter

cs

 

시간 초과남, 따라서 선형 리스트인 스택을 사용하여 문제를 접근하였고 해결하였음

 

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

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

package bj;

 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Stack;

import java.util.StringTokenizer;

public class p1406 {

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

         

         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

         String str = br.readLine();

         

         int n = Integer.parseInt(br.readLine());

         Stack<Character> st1 = new Stack<>();

         Stack<Character> st2 = new Stack<>();

         

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

             st1.add(str.charAt(i));

         }

         

         for(int i=0; i<n; i++) {

             StringTokenizer st = new StringTokenizer(br.readLine());

             char ch = st.nextToken().charAt(0);

             

             if(ch == 'L') {

                if(!st1.isEmpty()) {

                    char c = st1.pop();

                    st2.push(c);

                }

             }else if(ch == 'D') {

                 if(!st2.isEmpty()) {

                     char c = st2.pop();

                     st1.push(c);

                 }

             }else if(ch =='B') {

                 if(!st1.isEmpty())

                     st1.pop();

                 

             }else if(ch == 'P') {

                 char tmp = st.nextToken().charAt(0);

                 st1.push(tmp);

             }

         }

         

         while(!st1.isEmpty())

             st2.push(st1.pop());

         

         while(!st2.isEmpty()) {

             System.out.print(st2.pop());

         }

         

         

     }

}

 

Colored by Color Scripter

cs

'백준' 카테고리의 다른 글

#백준_11728 배열 합치기 - Java  (0) 2020.01.11
#백준_2146 다리 만들기 - Java  (0) 2020.01.09
#백준_1967 트리의 지름 - Java  (0) 2020.01.09
#백준_1167 트리의 지름 - Java  (0) 2020.01.09
#백준_11652 카드  (0) 2020.01.01

+ Recent posts