PS/BaekJoon

1543 JAVA ๋ฌธ์„œ๊ฒ€์ƒ‰

chaerlo127 2024. 2. 15. 20:13
728x90
 

1543๋ฒˆ: ๋ฌธ์„œ ๊ฒ€์ƒ‰

์„ธ์ค€์ด๋Š” ์˜์–ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์–ด๋–ค ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์„ธ๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์„ธ์ค€์ด์˜ ํ•จ์ˆ˜๋Š” ์ค‘๋ณต๋˜์–ด ์„ธ๋Š” ๊ฒƒ์€ ๋นผ๊ณ  ์„ธ์•ผ ํ•œ

www.acmicpc.net

๐Ÿ€ ๋ฌธ์ œ

์„ธ์ค€์ด๋Š” ์˜์–ด๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์–ด๋–ค ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ์–ด๋–ค ๋‹จ์–ด๊ฐ€ ์ด ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์„ธ๋ ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜, ์„ธ์ค€์ด์˜ ํ•จ์ˆ˜๋Š” ์ค‘๋ณต๋˜์–ด ์„ธ๋Š” ๊ฒƒ์€ ๋นผ๊ณ  ์„ธ์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์„œ๊ฐ€ abababa์ด๊ณ , ๊ทธ๋ฆฌ๊ณ  ์ฐพ์œผ๋ ค๋Š” ๋‹จ์–ด๊ฐ€ ababa๋ผ๋ฉด, ์„ธ์ค€์ด์˜ ์ด ํ•จ์ˆ˜๋Š” ์ด ๋‹จ์–ด๋ฅผ 0๋ฒˆ๋ถ€ํ„ฐ ์ฐพ์„ ์ˆ˜ ์žˆ๊ณ , 2๋ฒˆ๋ถ€ํ„ฐ๋„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋™์‹œ์— ์…€ ์ˆ˜๋Š” ์—†๋‹ค.

์„ธ์ค€์ด๋Š” ๋ฌธ์„œ์™€ ๊ฒ€์ƒ‰ํ•˜๋ ค๋Š” ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ๋‹จ์–ด๊ฐ€ ์ตœ๋Œ€ ๋ช‡ ๋ฒˆ ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ๋“ฑ์žฅํ•˜๋Š”์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

๐Ÿ€ ์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์„œ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์„œ์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 2500์ด๋‹ค. ๋‘˜์งธ ์ค„์— ๊ฒ€์ƒ‰ํ•˜๊ณ  ์‹ถ์€ ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด ๊ธธ์ด๋Š” ์ตœ๋Œ€ 50์ด๋‹ค. ๋ฌธ์„œ์™€ ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์™€ ๊ณต๋ฐฑ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

๐Ÿ€ ์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ์ค‘๋ณต๋˜์ง€ ์•Š๊ฒŒ ์ตœ๋Œ€ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.

๐Ÿ€ ์˜ˆ์ œ ์ž…๋ ฅ

์—์ œ ์ž…๋ ฅ 1

ababababa
aba

์˜ˆ์ œ ์ถœ๋ ฅ 1

2

์—์ œ ์ž…๋ ฅ 2

a a a a a
a a

์˜ˆ์ œ ์ถœ๋ ฅ 2

2

์—์ œ ์ž…๋ ฅ 3

ababababa
ababa

์˜ˆ์ œ ์ถœ๋ ฅ 3

1

์—์ œ ์ž…๋ ฅ 4

aaaaaaa
aa

์˜ˆ์ œ ์ถœ๋ ฅ 4

3

๐Ÿ€ ์ฝ”๋“œ

ํ‹€๋ฆฐ ์ฝ”๋“œ

import java.io.*;

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

        int si = 0;
        int answer = 0;

        for (int i = 0 ; i < first.length() ; i++){
            if(second.charAt(si) == first.charAt(i)) si++;
            else if(second.charAt(0) == first.charAt(i)) si = 1;
            else si = 0;

            if(si == second.length()){
                answer++;
                si = 0;
            }
        }

        System.out.println(answer);
    }
}

์œ„ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐ˜๋ก€๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

abababc
ababc

์ด๋ฏธ abab๊นŒ์ง€ ๊ฐ™๊ณ  ๋‹ค์Œ c๊ฐ€ ์—†์–ด์„œ si ๊ฐ€ 0์ด ๋˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ๋ฌธ์ž๋Š” ๋‹ค์‹œ ๋˜๋Œ์•„์˜ค์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ƒํ™ฉ์—์„œ๋Š” ์ œ๋Œ€๋กœ ๊ฐ’์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ์ด๋‹ค.

๋งž์€ ์ฝ”๋“œ

import java.io.*;

class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String first = br.readLine();
        String second = br.readLine();
        String rf = first.replace(second, "");

        System.out.println((first.length() - rf.length()) / second.length());
        
    }
}
728x90