[Java] SWEA 비밀번호 & 회문2

Lee GaEun·2025년 5월 2일

[Java] 알고리즘

목록 보기
71/93

1234 비밀번호 문제 링크

#1

15분


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            String a = sc.next();

            int aL = N+1;
            String now = "";
            while (true) {
                if(a.length() == aL) break;
                aL = a.length();
                for(int i=0; i<=9; i++) {
                    now = String.valueOf(i);
                    now += String.valueOf(i);
                    a = a.replaceAll(now, "");
                }
            }

            System.out.println("#" + test_case + " " + a);
        }
    }
}

  • 성공!
  • 초기화 조심!

1216 회문2 문제 링크

#1

43분


import java.util.HashMap;
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.Set;

class Solution
{
    static char[][] arr;
    public static void main(String args[]) throws Exception
    {
        Scanner sc = new Scanner(System.in);
        int T;
        T=10;

        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            String a;
            arr = new char[100][100];
            for(int i=0; i<100; i++) {
                a = sc.next();
                for(int j=0; j<100; j++) {
                    arr[i][j] = a.charAt(j);
                }
            }

            int answer = 0;
            for(int i=100; i>=0; i--) { // 회문의 길이 별 탐색
                answer = findS(i);
                if(answer != 0) break;
            }

            System.out.println("#"+N+" "+answer);
        }
    }

    static int findS(int T) {
        int a = 0;
        int b = 0;
        for(int i=0; i<100; i++) {
            for(int j=0; j<=100-T; j++) {  // 배열 전부 돌기
                for(int k=0; k<T/2; k++) { // 회문인지
                    if(arr[i][j+k] == arr[i][j+T-1-k]) a++;  // 가로
                    if(arr[j+k][i] == arr[j+T-1-k][i]) b++;  // 세로
                }

                if(a == T/2) return T;
                if(b == T/2) return T;
                a = 0;
                b = 0;
            }
        }
        return 0;
    }
}

  • 회문1을 푼 지가 얼마 안 되서 금방 풀었는데
  • 이 문제는 특이하게 출력구에서 test 번호를 N으로 찍어줘야됨...
  • 그것때문에 시간 다 버림.. 댓글 보고 알았음...
profile
I will give it my all (๑•̀o•́๑)ง

0개의 댓글