-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16916.java
More file actions
52 lines (45 loc) · 1.27 KB
/
16916.java
File metadata and controls
52 lines (45 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* 백준 16916번 부분 문자열
* KMP 알고리즘
*/
import java.util.*;
import java.io.*;
public class Main {
static int pi[] = new int[1000001];
static void makePi(String p){
int index = 0;
for(int i=1; i<p.length(); i++){
while(index > 0 && p.charAt(i)!=p.charAt(index)){
index = pi[index - 1];
}
if(p.charAt(index) == p.charAt(i)){
pi[i] = ++index;
}
}
}
static Boolean kmp(String s, String p){
int index = 0;
int cnt = 0;
for(int i=0; i<s.length(); i++){
while(index > 0 && s.charAt(i) != p.charAt(index)){
index = pi[index-1];
}
if(s.charAt(i)==p.charAt(index)){
if(index == p.length()-1){
cnt++;
index = pi[index];
}
else
index++;
}
}
return cnt > 0;
}
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
String p = br.readLine();
makePi(p);
System.out.println(kmp(s,p) ? 1 : 0);
}
}