Repository navigation
Expand file tree
/
Copy pathUnconcatenateString.java
More file actions
64 lines (49 loc) · 1.73 KB
/
Copy pathUnconcatenateString.java
File metadata and controls
64 lines (49 loc) · 1.73 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
import java.util.HashMap;
import java.util.Hashtable;
import CtCILibrary.AssortedMethods;
import CtCILibrary.Trie;
public class UnconcatenateString {
public static Trie dictionary;
static HashMap<Integer, Integer> cost = new HashMap<Integer, Integer>();
public static int getCost(String s){
if(s.isEmpty()) return 0;
return dictionary.contains(s, true)? 0: s.length();
}
// return opt(start, s.length-1)
// prev= substring(start, i+1)
public static int process(String s, int i, int start){
int res=0;
if(i == s.length()-1) res = getCost(s.substring(start));
else if(cost.containsKey(start)){
return cost.get(start);
}
else if(!dictionary.contains(s.substring(start, i+1), false)){
return i+1-start + process(s, i+1, i+1);
}
else{
int c11 = getCost(s.substring(start, i+1));
int c12 = process(s, i+1, i+1);
int c2 = process(s, i+1, start);
res = Math.min(c11+c12, c2);
}
cost.put(start, res);
return res;
}
public static String clean(String str) {
char[] punctuation = {',', '"', '!', '.', '\'', '?', ','};
for (char c : punctuation) {
str = str.replace(c, ' ');
}
return str.replace(" ", "").toLowerCase();
}
public static void main(String[] args) {
dictionary = AssortedMethods.getTrieDictionary();
String sentence = "As one of the top companies in the world, Google will surely attract the attention of computer gurus. This does not, however, mean the company is for everyone.";
sentence = clean(sentence);
System.out.println(sentence);
//Result v = parse(0, 0, new Hashtable<Integer, Result>());
//System.out.println(v.parsed);
int v = process(sentence, 0, 0);
System.out.println(v);
}
}