본문 바로가기
알고리즘/SWEA

1989. 초심자의 회문 검사.D2

by 모두의 향연 2022. 2. 12.
728x90
반응형
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
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Solution_D2_1989_초심자의회문검사 {
//    static String str = "10\r\n" + "level\r\n" + "samsung\r\n" + "eye\r\n" + "exo\r\n" + "ioi\r\n" + "blackpink\r\n"
//            + "hannah\r\n" + "B1A4\r\n" + "linetown\r\n" + "nursesrun";
 
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//        br = new BufferedReader(new StringReader(str));
        StringBuilder sb = new StringBuilder();
        StringTokenizer st;
 
        int T = Integer.parseInt(br.readLine());
        for (int tc = 1; tc <= T; tc++) {
            sb.append("#").append(tc).append(" ");
 
            st = new StringTokenizer(br.readLine());// 라인으로 쫙 읽어
            String str = st.nextToken();// 그걸 토큰으로 분류시켜
//            System.out.println(str);//확인
            char[] chArr = new char[str.length()];
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                chArr[i] = c;
//                System.out.println(Arrays.toString(chArr));//확인
            }
            
            int count = 0;
            if (chArr.length % 2 == 0) {// 짝수
                for (int i = 0; i < chArr.length / 2; i++) {
                    if (chArr[i] == chArr[chArr.length - 1 - i]) {
                        count++;
                    }
                }
                if(count==chArr.length/2) {
                    sb.append("1").append("\n");
                }else {
                    sb.append("0").append("\n");
                }
            } else {// 홀수
                for (int i = 0; i < chArr.length / 2; i++) {
                    if (chArr[i] == chArr[chArr.length - 1 - i]) {
                        count++;
                    }
                    if (i == chArr.length / 2) count++;
                }
                if(count==chArr.length/2) {
                    sb.append("1").append("\n");
                }else {
                    sb.append("0").append("\n");
                }
            }
        }
        System.out.println(sb);
    }
}
 
cs
728x90
반응형