-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
56 lines (45 loc) · 1.81 KB
/
Main.java
File metadata and controls
56 lines (45 loc) · 1.81 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
53
54
55
56
package Esame;
import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String textFile = "src/Esame/testKMP.txt";
// Chiede all'utente di inserire il pattern
Scanner scanner = new Scanner(System.in);
System.out.print("Inserisci il pattern da cercare: ");
String pattern = scanner.nextLine();
try {
// Leggi tutto il contenuto dei file, rimuovendo eventuali ritorni a capo
String text = Files.readString(Paths.get(textFile)).replaceAll("\\r?\\n", "");
if (pattern.isEmpty()) {
System.out.println("Errore: il pattern è vuoto.");
return;
}
if (text.isEmpty()) {
System.out.println("Errore: il testo da analizzare è vuoto.");
return;
}
if (pattern.length() > text.length()) {
System.out.println("Errore: il pattern è più lungo del testo.");
return;
}
// Esegui la ricerca con KMP
KMPMatcher matcher = new KMPMatcher();
List<Integer> positions = matcher.search(text, pattern);
if (positions.isEmpty()) {
System.out.println("Pattern non trovato.");
} else {
System.out.print("Pattern trovato alle posizioni: ");
for (int pos : positions) {
System.out.print(pos + " ");
}
System.out.println();
}
} catch (IOException e) {
System.err.println("Errore nella lettura dei file: " + e.getMessage());
}
}
}