๊ธฐ๋ก๋ฐฉ

BOJ_25501 : ์žฌ๊ท€์˜ ๊ท€์žฌ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_25501 : ์žฌ๊ท€์˜ ๊ท€์žฌ

Soom_1n 2023. 1. 20. 23:15

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

25501๋ฒˆ: ์žฌ๊ท€์˜ ๊ท€์žฌ

๊ฐ ํ…Œ์ŠคํŠธ์ผ€์ด์Šค๋งˆ๋‹ค, isPalindrome ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜๊ฐ’๊ณผ recursion ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ํšŸ์ˆ˜๋ฅผ ํ•œ ์ค„์— ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ž…๋ ฅ๋ฐ›์€ ๋ฌธ์ž์—ด์ด ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ์ง€ ํŒ๋ณ„ํ•œ๋‹ค.
  • ์ฃผ์–ด์ง„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ๊ทธ ์ค‘ recursion ๋ฉ”์„œ๋“œ๊ฐ€ ๋ช‡ ๋ฒˆ ์‚ฌ์šฉ ๋๋Š”์ง€๋„ ๊ฐ™์ด ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.util.Scanner;

public class Main {
    private static int count;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        for (int i = 0; i < t; i++) {
            count = 0;
            System.out.println(isPalindrome(sc.next()) + " " + count);
        }
    }
    private static int recursion(String s, int l, int r){
        count++;
        if(l >= r) return 1;
        else if(s.charAt(l) != s.charAt(r)) return 0;
        else return recursion(s, l+1, r-1);
    }
    private static int isPalindrome(String s) {
        return recursion(s, 0, s.length()-1);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์ฃผ์–ด์ง„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํŒฐ๋ฆฐ๋“œ๋กฌ์„ ํŒ๋‹จํ–ˆ๊ณ , ์ •์  ๋ณ€์ˆ˜ count๋ฅผ ์‚ฌ์šฉํ•ด recursion ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ํšŸ์ˆ˜๋ฅผ ์ฒดํฌํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฐฑ์ค€ ๋‹จ๊ณ„๋ณ„ ๋ฌธ์ œ์˜ ์žฌ๊ท€ํŒŒํŠธ 3๋ฒˆ์งธ ๋ฌธ์ œ๋ฅผ ๊ฐ„๋‹จํžˆ ํ’€์—ˆ๋‹ค.

728x90