第一題
/*
* 思路
* 1.將keyword 存儲到list 中
* 2.按行讀取到input中的數(shù)據(jù)每一行進(jìn)行l(wèi)ist匹配
*
* 這題通過abc查找的bc的時候,沒有更好的優(yōu)化思路 求解
*
* */
public static void main(String[] args) {
printResult("./input.txt" , "./keyword.conf");
}
public static void printResult(String inputPath, String keyword) {
Set<String> conf = getKeyword(keyword);
printResult(inputPath, conf);
}
/*
* 匹配并輸出打印輸出
* */
private static void printResult(String inputFilePath, Set<String> keys) {
try (FileInputStream inputStream = new FileInputStream(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
String str;
while ((str = bufferedReader.readLine()) != null) {
//與每行的數(shù)據(jù)與keyword 進(jìn)行匹配 求解更優(yōu)的算法
for (String key : keys) {
if (str.split(" ")[0].contains(key)) {
System.out.println(str);
break;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 存儲keyword
* */
private static Set<String> getKeyword(String filePath) {
Set<String> set = new HashSet<>();
try (FileInputStream inputStream = new FileInputStream(filePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
String str;
while ((str = bufferedReader.readLine()) != null)
set.add(str);
} catch (IOException e) {
e.printStackTrace();
}
return set;
}
第二題
/*
* 思路
*
* 創(chuàng)建一個大小為1000 int數(shù)組
* 其中 int[0] 代表 0—99
* 其中 int[1] 代表 100—199
* 其中 int[2] 代表 200—299
* ……
* 依次類推
* 時間復(fù)雜為 O(n)
* */
public static void main(String[] args) {
printResult("./input.txt");
}
private static void printResult(String filePath) {
int n = 100000;
int m = 100;
int[] memo = new int[n / m];
storageResult(memo, filePath);
int temp;
for (int i = 0; i < memo.length; i++) {
temp = i * 100;
System.out.println(temp + "—" + (temp + 99) + " " + memo[i]);
}
}
private static void storageResult(int[] memo, String inputFilePath) {
try (FileInputStream inputStream = new FileInputStream(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
String str;
while ((str = bufferedReader.readLine()) != null) {
memo[Integer.parseInt(str) / 100]++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
第三題
/*
*思路
* 按行讀取每一條數(shù)據(jù),將該數(shù)據(jù)以Map的方式形如下圖
*
cn com
/ / \
com baidu tencent
/
sina
/
sports
將數(shù)據(jù)進(jìn)行壓縮
將"roll.sports.sina.com.cn"
解析為 數(shù)組 先找到 cn 的 HashMap 查看是否存在 tempMap,
在tempMap中查找key為com的HashMap,
如此查找下去,知道查找的key 不存在,輸出找到的結(jié)果。
由于 數(shù)據(jù)量比較大,可以將通過Hash的方式將數(shù)據(jù)存儲在多個機(jī)器上,例如com 和cn,結(jié)尾的數(shù)據(jù)比較多,可以將其分配2臺甚至多臺機(jī)器處理,或者內(nèi)存更大的機(jī)器
基本的思路還是不變的
* 時間復(fù)雜度O(n)
* */
public static void main(String[] args) {
String data = "roll.sports.sina.com.cn";
HashMap<String, HashMap> stringHashMapHashMap = storageDate("./input.txt");
String[] arr = data.split("\\.");
StringBuilder stringBuilder = new StringBuilder();
for (int i = arr.length - 1; i > 0; i--) {
if (stringHashMapHashMap.containsKey(arr[i])) {
stringBuilder.insert(0, "." + arr[i]);
stringHashMapHashMap = stringHashMapHashMap.get(arr[i]);
} else {
break;
}
}
System.out.println(stringBuilder.insert(0, "*"));
}
/*
* 將所有數(shù)據(jù)壓縮插入到map中
* */
private static HashMap<String, HashMap> storageDate(String inputFilePath) {
HashMap<String, HashMap> map = new HashMap<>();
try (FileInputStream inputStream = new FileInputStream(inputFilePath);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) {
String str;
while ((str = bufferedReader.readLine()) != null) {
String[] arr = str.split("\\.");
addToMap(arr, map, arr.length - 1);
}
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
/*
* 將數(shù)據(jù)插入到Map中
* */
private static HashMap<String, HashMap> addToMap(String[] arr, HashMap<String, HashMap> map, int n) {
if (n >= 1) {
map.put(arr[n], addToMap(arr, map.containsKey(arr[n]) ? map.get(arr[n]) : new HashMap<>(), n - 1));
}
return map;
}