From 88bdec03fea92c4ab316d0de53e372593eb829f9 Mon Sep 17 00:00:00 2001 From: HeeEul Shin <83682424+ShinHeeEul@users.noreply.github.com> Date: Wed, 12 Mar 2025 10:52:22 +0900 Subject: [PATCH] =?UTF-8?q?[20250312]=20BOJ=20/=20P5=20/=20=EC=88=9C?= =?UTF-8?q?=ED=99=98=20=EC=88=9C=EC=97=B4=20/=20=EC=8B=A0=ED=9D=AC?= =?UTF-8?q?=EC=9D=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\355\231\230 \354\210\234\354\227\264.md" | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 "ShinHeeEul/202503/12 BOJ \354\210\234\355\231\230 \354\210\234\354\227\264.md" diff --git "a/ShinHeeEul/202503/12 BOJ \354\210\234\355\231\230 \354\210\234\354\227\264.md" "b/ShinHeeEul/202503/12 BOJ \354\210\234\355\231\230 \354\210\234\354\227\264.md" new file mode 100644 index 00000000..d119a071 --- /dev/null +++ "b/ShinHeeEul/202503/12 BOJ \354\210\234\355\231\230 \354\210\234\354\227\264.md" @@ -0,0 +1,64 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + + + static int[] failure; + static int count = 0; + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + + String a = br.readLine(); + a += a.substring(0, a.length() - 1); + String b = br.readLine(); + + char[] ac = a.toCharArray(); + char[] bc = b.toCharArray(); + + failure = new int[ac.length]; + + failureFunction(bc); + kmp(ac, bc); + System.out.println(count); + } + + public static void failureFunction(char[] s) { + int p = 0; + + for(int idx = 1; idx < s.length; idx++) { + + while(p != 0 && s[idx] != s[p]) { + p = failure[p - 1]; + } + + if(s[idx] == s[p]) { + p++; + failure[idx] = p; + } + } + + } + + public static void kmp(char[] s1, char[] s2) { + + int p = 0; + + for(int idx = 0; idx < s1.length; idx++) { + + while(p != 0 && s1[idx] != s2[p]) p = failure[p - 1]; + + if(s1[idx] == s2[p]) { + if(p == s2.length - 1) { + count++; + p = failure[p]; + } else { + p++; + } + } + } + } +} +```