-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPart4.java
More file actions
67 lines (63 loc) · 1.94 KB
/
Copy pathPart4.java
File metadata and controls
67 lines (63 loc) · 1.94 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
57
58
59
60
61
62
63
64
65
66
67
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.SortedSet;
import java.util.TreeSet;
public class Part4 {
/**
* @param r the reader to read from
* @param w the writer to write to
* @throws IOException
*/
public static void doIt(BufferedReader r, PrintWriter w)
throws IOException {
String line = r.readLine();
SortedSet<String> sortedSuffixes = new TreeSet<>();
sortedSuffixes.add("");
while(line != null){
String cpyLine = line;
SortedSet<String> greaterThan = sortedSuffixes.tailSet(line);
if(!greaterThan.isEmpty()){
if(greaterThan.first().contains(line)){
w.println(line);
}
}
for(int i = 0; i < cpyLine.length(); i++){
sortedSuffixes.add(line.substring(i, line.length()));
}
line = r.readLine();
}
}
/**
* The driver. Open a BufferedReader and a PrintWriter, either from System.in
* and System.out or from filenames specified on the command line, then call doIt.
* @param args
*/
public static void main(String[] args) {
try {
BufferedReader r;
PrintWriter w;
if (args.length == 0) {
r = new BufferedReader(new InputStreamReader(System.in));
w = new PrintWriter(System.out);
} else if (args.length == 1) {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(System.out);
} else {
r = new BufferedReader(new FileReader(args[0]));
w = new PrintWriter(new FileWriter(args[1]));
}
long start = System.nanoTime();
doIt(r, w);
w.flush();
long stop = System.nanoTime();
System.out.println("Execution time: " + 1e-9 * (stop-start));
} catch (IOException e) {
System.err.println(e);
System.exit(-1);
}
}
}