-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task1.java
63 lines (52 loc) · 1.59 KB
/
Task1.java
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
53
54
55
56
57
58
59
60
61
62
63
/* Kornilov Nikita, M3102, 09.05.2021 */
package Sem2.Lab13;
import java.io.*;
import java.util.*;
public class Task1 {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public static void main(String[] args) {
String fileName = "search1";
new Task1().run(String.format("%s.in", fileName), String.format("%s.out", fileName));
}
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
String inputString = br.readLine();
if (inputString != null) {
in = new StringTokenizer(inputString);
}
else {
return null;
}
}
return in.nextToken();
}
public void solve() throws IOException {
char[] p = nextToken().toCharArray(), t = nextToken().toCharArray();
ArrayList<Integer> pos = new ArrayList<>();
out : for (int i = 0; i <= t.length - p.length; i++) {
for (int j = 0; j < p.length; j++) {
if (t[i + j] != p[j]) {
continue out;
}
}
pos.add(i + 1);
}
out.println(pos.size());
for (int i : pos) {
out.print(i + " ");
}
}
public void run(String inputFile, String outputFile) {
try {
br = new BufferedReader(new FileReader(inputFile));
out = new PrintWriter(outputFile);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}