반응형
public class Q_9093 {
    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        try {
            int count = Integer.parseInt(br.readLine());//테스트 케이스 갯수
            for(int idx = 0; idx < count; idx++) {
                String input = br.readLine();

                for (int x = 0; x < input.length(); x++) {
                    String cc = input.substring(x, x+1);
                    stack.push(cc);

                    if(cc.equals(" ") || x == input.length()-1){
                        StringBuilder rWord = new StringBuilder();
                        String separator = (x == input.length()-1) ? "\n" : "";

                        while(!stack.empty()) {
                            String temp = stack.pop();
                            if(temp.equals(" ")) {
                                separator = temp;
                            }else {
                                rWord.append(temp);
                            }
                        }
                        rWord.append(separator);
                        System.out.print(rWord);
                    }
                }
            }
            br.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

URL :  https://www.acmicpc.net/problem/9093

반응형

+ Recent posts