๊ธฐ๋ก๋ฐฉ

BOJ_1120 : ๋ฌธ์ž์—ด ๋ณธ๋ฌธ

CodingTest/Java

BOJ_1120 : ๋ฌธ์ž์—ด

Soom_1n 2022. 11. 2. 22:21

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

 

1120๋ฒˆ: ๋ฌธ์ž์—ด

๊ธธ์ด๊ฐ€ N์œผ๋กœ ๊ฐ™์€ ๋ฌธ์ž์—ด X์™€ Y๊ฐ€ ์žˆ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด X์™€ Y์˜ ์ฐจ์ด๋Š” X[i] ≠ Y[i]์ธ i์˜ ๊ฐœ์ˆ˜์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, X=”jimin”, Y=”minji”์ด๋ฉด, ๋‘˜์˜ ์ฐจ์ด๋Š” 4์ด๋‹ค. ๋‘ ๋ฌธ์ž์—ด A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, A์˜

www.acmicpc.net



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

  • ๋ฌธ์ž์—ด A์™€ B๊ฐ€ ์ฃผ์–ด์ง€๊ณ , A๋ฅผ ์ตœ๋Œ€ํ•œ B์— ๋น„์Šทํ•˜๊ฒŒ ์•ž ๋’ค๋กœ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. ๋ฌธ์ž๊ฐ€ ๋‹ค๋ฅธ ์ธ๋ฑ์Šค์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
    • A๋Š” B๋ณด๋‹ค ๊ธธ์ด๊ฐ€ ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.
  • ๋ฌธ์ž์—ด B์—์„œ ๋ฌธ์ž์—ด A๋ฅผ ๊ฒน์ณค์„๋•Œ ๊ฐ€์žฅ ๋งŽ์ด ๊ฒน์น˜๋Š” ๋ถ€๋ถ„์„ ์ฐพ๋Š”๋‹ค.
  • ๋ฌธ์ž์—ด A์˜ ๊ธธ์ด์—์„œ ๊ฒน์นœ ๋ถ€๋ถ„์˜ ์ˆ˜๋ฅผ ๋บ€ ๊ฐ’์ด ๊ฐ€์žฅ ํด ๋•Œ๋ฅผ ์ฐพ์•„ ์ถœ๋ ฅํ•œ๋‹ค.

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        String a = st.nextToken();
        String b = st.nextToken();
        int answer = 0;

        for (int i = 0; i <= b.length() - a.length(); i++){
            int count = 0;
            for (int j = 0; j < a.length(); j++){
                if (a.charAt(j) == b.charAt(i+j)) count++;
            }
            if (count > answer)
                answer = count;
        }
        System.out.println(a.length() - answer);
    }
}

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

  • b๊ธธ์ด์—์„œ a๋ฅผ ๋บ€ ๋งŒํผ ๋ฐ˜๋ณตํ•ด์„œ i๋ฅผ 1์”ฉ ๋Š˜๋ฆฐ๋‹ค. ๋ฌธ์ž์—ด b์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
    • a์™€ b์˜ ์ผ๋ถ€๋ถ„์„ ๋น„๊ตํ•ด์„œ ๊ฐ™์€ ์ธ๋ฑ์Šค์—์„œ ๊ฐ™์€ ๋ฌธ์ž์ผ ๋•Œ๋ฅผ count์— ์„ผ๋‹ค.
    • ๊ฐ€์žฅ ํฐ count๋ฅผ answer์— ์ €์žฅํ•œ๋‹ค.
  • a๊ธธ์ด์—์„œ ๋น„์Šทํ•œ ๋ถ€๋ถ„์˜ ์ˆ˜์ธ answer๋ฅผ ๋นผ์„œ ๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ๋ฌธ์ œ ๊ทœ์น™์—๋”ฐ๋ผ ๋ฌธ์ž์—ด ์•ž ๋’ค๋กœ ์ถ”๊ฐ€ํ•ด๊ฐ€๋ฉฐ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฐพ์•„์•ผ ํ•˜๋Š” ์ค„ ์•Œ๊ณ  ์–ด๋ ต๊ฒŒ ๋Š๊ปด์กŒ๋‹ค.
  • ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ ‘๊ทผํ•ด๋ณด๋‹ˆ ์ƒ๊ฐ๋ณด๋‹ค ์‰ฝ๊ฒŒ ํ’€๋ ธ๋‹ค.

728x90