到第三步,已经把算法优化的精髓掌握了,用树形结构来承载。
但是有些细节不容忽视,我们大量使用了ArrayList.contains,效率非常不高。
这里进行进一步优化,就是“疯狂的排序、疯狂的二分查找”。
package test;
import static util.PrintUtil.print;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
static String[] keys = {"办证", "气枪出售", "裸聊", "裸表演", "土枪卖"};
static String tContent = "再办证顶";
static ArrayList<String> first = new ArrayList<String>();
static String[] sortFirst;
static HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
static HashMap<String, String[]> sortMap = new HashMap<String, String[]>();
static {
ArrayList<String> temp;
String key, value;
int length;
for (String k : keys) {
if (!first.contains(k.substring(0, 1))) {
first.add(k.substring(0, 1));
}
length = k.length();
for (int i = 1; i < length; i ++) {
key = k.substring(0, i);
value = k.substring(i, i + 1);
if (i == 1 && !first.contains(key)) {
first.add(key);
}
// 有,添加
if (map.containsKey(key)) {
if (!map.get(key).contains(value)) {
map.get(key).add(value);
}
}
// 没有添加
else {
temp = new ArrayList<String>();
temp.add(value);
map.put(key, temp);
}
}
}
sortFirst = first.toArray(new String[first.size()]);
Arrays.sort(sortFirst); // 排序
String[] sortValue;
ArrayList<String> v;
Map.Entry<String, ArrayList<String>> entry;
Iterator<Entry<String, ArrayList<String>>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
entry = (Map.Entry<String, ArrayList<String>>) iter.next();
v = (ArrayList<String>)entry.getValue();
sortValue = v.toArray(new String[v.size()]);
Arrays.sort(sortValue); // 排序
sortMap.put(entry.getKey(), sortValue);
}
}
/**
* 快速实现的方法
*/
public final static String test1(String content) {
for (String k : keys) {
if (content.indexOf(k) > -1)
return k;
}
return null;
}
/**
* 优化一
*/
public final static String test2(String content) {
boolean bFirst = false;
int length = content.length();
for (int i = 0; i < length; i ++) {
if (first.contains(content.substring(i, i + 1))) {
bFirst = true;
break;
}
}
return bFirst ? test1(content) : null;
}
/**
* 优化二
*/
public final static String test3(String content) {
String r = null, f, g, c = content;
ArrayList<String> temps;
int length = c.length();
tag : for (int i = 0; i < length - 1; i++) {
f = c.substring(i, i + 1);
if (first.contains(f)) {
for (int j = i + 1; j < length; j++) {
f = c.substring(i, j);
g = c.substring(j, j + 1);
temps = map.get(f);
if (temps == null) { // 找到了
print("ok");
r = f;
break tag;
}
if (temps.contains(g)) {
if (j == length - 1) {
print("find!");
r = c.substring(i, j + 1);
break tag;
}
} else { // 没有找到了
break;
}
}
}
}
return r;
}
/**
* 优化三
*/
public final static String test4(String content) {
String r = null, f, g, c = content;
String[] temps;
int length = c.length();
tag : for (int i = 0; i < length - 1; i++) {
f = c.substring(i, i + 1);
// 二分查找
if (Arrays.binarySearch(sortFirst, f) > -1) {
for (int j = i + 1; j < length; j++) {
f = c.substring(i, j);
g = c.substring(j, j + 1);
temps = sortMap.get(f);
if (temps == null) { // 找到了
print("ok");
r = f;
break tag;
}
// 二分查找
if (Arrays.binarySearch(temps, g) > -1) {
if (j == length - 1) {
print("find!");
r = c.substring(i, j + 1);
break tag;
}
} else { // 没有找到了
break;
}
}
}
}
return r;
}
public static void main(String[] args) {
long time1 = System.currentTimeMillis();
print(test4(tContent));
print("test4 time:" + (System.currentTimeMillis() - time1));
}
}
来源:oschina
链接:https://my.oschina.net/u/113867/blog/32385