Home BOJ. Find (1786)
Post
Cancel

BOJ. Find (1786)

[Link] https://www.acmicpc.net/problem/1786


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
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		String t = br.readLine(), p = br.readLine();
		int[] pi = new int[p.length()];
		ArrayList<Integer> al = new ArrayList<>();
		getPi(pi, p);
		int l = 0, r = 0, cnt = 0;

		while(l < t.length()) {
			while(r > 0 && t.charAt(l) != p.charAt(r)) r = pi[r - 1];
			if(t.charAt(l) == p.charAt(r)) {
				if(r == p.length() - 1) {
					cnt++;
					al.add(l + 2 - p.length());
					r = pi[r];
				}
				else r++;
			}
			l++;
		}

		println(cnt);
		for(int e: al) sb.append(e).append(" ");
		print(sb);
	}

	static public void getPi(int[] pi, String p) {
		int l = 0;
		for(int r = 1; r < p.length(); r++) {
			while(l > 0 && p.charAt(l) != p.charAt(r)) l = pi[l - 1];
			if(p.charAt(l) == p.charAt(r)) pi[r] = ++l;
		}
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.