-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
76 lines (66 loc) · 1.62 KB
/
Main.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
64
65
66
67
68
69
70
71
72
73
74
75
76
package cpsc320;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
public class Main {
static String[] tokens;
static int[] OPT;
static int optimalLineLength = 10;
static int numOfWords;
static int[][] slack;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File file = new File(args[0]);
FileReader in = new FileReader(file);
char[] buffer = new char[500];
while (in.read(buffer) != -1)
;
in.close();
String words = String.copyValueOf(buffer);
String delims = "[ ]+";
tokens = words.split(delims);
numOfWords= tokens.length;
OPT = new int [numOfWords];
slack = new int [numOfWords][numOfWords];
findSlack();
int x = FindOptRecursive(numOfWords-1);
System.out.println(x);
}
public static int FindOptRecursive(int n) {
if (n == 0) {
return 0;
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < i; j++) {
OPT[j] = (slack[i][j] + FindOptRecursive( j - 1));
}
}
return OPT[n];
}
public static int Memoization(int n) {
OPT[0] = 0;
for (int i=0; i < n; i++) {
for (int j = 0; j < i; j++ ) {
OPT[i] = slack[i][j]+ OPT[j -1];
}
}
return OPT[n];
}
public static void findSlack() {
int total;
int slackPerLine;
for (int i = 0; i < slack.length ; i++) {
total = 0;
for (int j = i; j <slack[i].length ; j++) {
total += tokens[j].length();
slackPerLine = optimalLineLength - total;
if (slackPerLine < 0) {
slack[i][j] = 10000; // invalidate it
} else {
slack[i][j] =slackPerLine;// * slackPerLine;
}
}
}
}
}