Jun's Development Journey

[SWEA] 1216. [S/W 문제해결 기본] String - 회문2 본문

SWEA/Intermediate

[SWEA] 1216. [S/W 문제해결 기본] String - 회문2

J_Jayce 2019. 8. 2. 14:03

문제

https://www.swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV14Rq5aABUCFAYi&categoryId=AV14Rq5aABUCFAYi&categoryType=CODE

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

 

풀이

 

이 문제는 회문 1코드를 응용하는 문제이다. 회문 1문제에서는 찾을 회문의 길이가 정해져 있지만, 이 문제는 회문의 길이별로 회문이 존재하는지 찾고, 그 길이의 최대값을 구하는 문제이다. 회문 1코드에서 len 부분을 반복문을 통해 1 부터 100까지 탐색해볼 수 있도록 구현했다.

 

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
import java.util.*;
 
public class Solution {
    static Scanner scan = new Scanner(System.in);
    static String[] sarr = new String[8];
    static int len = 0;
    static boolean is_palindrome(String test) {
        boolean chk = true;
        int len = test.length();
        for(int i=0;i<len/2;i++) {
            if(test.charAt(i)!=test.charAt(len-i-1)) {
                chk = false;
                break;
            }
        }
        return chk;
    }
    static int get_pali_num_row(int row) {//가로 문자열에서 탐색
        int answer = 0;
        String tmp = sarr[row];
        for(int i=0;i<=tmp.length()-len;i++) {
            if(is_palindrome(tmp.substring(i,i+len)))
                answer++;
        }
        return answer;
    }
    static int get_pali_num_col(int col) {//세로 문자열에서 탐색
        int answer = 0;
        String tmp = "";
        for(int i=0;i<8;i++)//열 번호를 받고 행 번호를 증가시키면서 세로 문자열을 생성
            tmp+= sarr[i].charAt(col);
        
        for(int i=0;i<=tmp.length()-len;i++) {
            if(is_palindrome(tmp.substring(i,i+len)))
                answer++;
        }
        return answer;
    }
    static void solution() {
        for(int i=1;i<=10;i++) {
            int sum=0;
            len = scan.nextInt();
            for(int j=0;j<8;j++)//문자열 입력 받음
                sarr[j] = scan.next();
            
            for(int j=0;j<8;j++)//가로 문자열과 세로 문자열 탐색해서 회문 갯수 찾아 더해줌
                sum +=(get_pali_num_row(j)+get_pali_num_col(j));
            
            System.out.println("#"+i+" "+sum);
        }
    }
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        solution();
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter