본문으로 바로가기

[백준 3568] iSharp -JAVA //le_effort//

category 알고리즘 2020. 3. 11. 17:43

구현 문제중 쉬운편에 속하지 않을까?

 

 

입력을 보자

기본적으로 int& 은 무조건 다 들어간다.

변수형과 변수명 사이에는 공백이 하나 있다고 하니 이걸 따로 저장해준다.

 

그 후 공통 변수형을 제외한 나머지들의 구분은 "  ,  "  와 "  ;  " 로 구분되는걸 알 수 있다.

 

if(str[i].charAt(j)==',' || str[i].charAt(j)==';') { // 답을 구성할때 필요없는것들은 skip
continue;
}

 

이런식으로 저 부분은 스킵을하고

';' 같은 경우는 제일 마지막에 따로 추가해줬다.

 

나는 역순으로 탐색하고 임시 문자열에 넣어주었다.

ex) a*[]&,  일 경우 탐색의 방향은 , & ] [ * a  이 순서대로 된다.

 

이렇게하면 &][*a 로 들어오는데 배열 문자열만 처리를 해주면 된다

역순으로 진행하니 ] 가 먼저나오니 그냥 [] 를 넣어주고 인덱스를 하나 줄여주었다.

 

1
2
3
4
5
if(str[i].charAt(j)==']') {        //역순으로 저장할 경우 ][  이런식으로 저장됨으로 처리를 해준다.
                    tmp+="[]";    
                    j--;
                    continue;
                }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
cs

 

그리고 변수형과 변수명 사이에 공백이 한 칸 있어야 함으로

나는 변수형을 따로 저장하고 변수명을 공백한칸주고 저장 받았다

 

즉 최종적으로

공통변수형+변수형+변수명 이렇게 하면 되는것이다.

 

그럼 변수명은 어떻게 따로 구분하냐면

 

1
2
3
4
5
6
public static boolean end(char c) {
        if(c!='*' && c!='&' && c!='[' && c!=']') {
            return true;
        }
        return false;
    }
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">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
import java.io.*;
import java.util.*;
public class Main {
    static String str[];
    static String ans[];
    static String std="";
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        
        String[] t = br.readLine().split(" ");
        std+=t[0];
        
        str = new String[t.length-1];
        ans = new String[t.length-1];    // 최종답
        for(int i=0; i<str.length; i++) {
            str[i] = t[i+1];
        }        // 가독성을 위해서 새로운 str배열에 담아줌
        
        
        
        for(int i=0; i<str.length; i++) {
            String tmp="";    // 여기다가 답을 담을것
            String alpha=" ";        // 변수의 이름을 담을 것
            for(int j=str[i].length()-1; j>=0; j--) {    // 역순으로 진행
                if(str[i].charAt(j)==',' || str[i].charAt(j)==';') {    // 답을 구성할때 필요없는것들은 skip
                    continue;
                }
                if(str[i].charAt(j)==']') {        //역순으로 저장할 경우 ][  이런식으로 저장됨으로 처리를 해준다.
                    tmp+="[]";    
                    j--;
                    continue;
                }
                if(end(str[i].charAt(j))) {
                    for(int k=0; k<=j; k++) {
                        alpha+=str[i].charAt(k);
                    }
                    alpha+=";";
                    break;
                }
                tmp+=str[i].charAt(j);
            }
            ans[i] = std+tmp+alpha;
        }
        for(int i=0; i<ans.length; i++) {
            System.out.println(ans[i]);
        }
    }
    public static boolean end(char c) {
        if(c!='*' && c!='&' && c!='[' && c!=']') {
            return true;
        }
        return false;
    }
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
color:white">cs