-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLZ_78.java
More file actions
65 lines (65 loc) · 2.92 KB
/
LZ_78.java
File metadata and controls
65 lines (65 loc) · 2.92 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
import java.util.ArrayList;
import java.util.Scanner;
public class LZ_78 {
public static int indexOfStringInDic(String word, ArrayList<String> Dictionary) {
return Dictionary.indexOf(word);
}
public static boolean StringNotFound(String word, ArrayList<String> arraySearch) {
for(int i=0;i<arraySearch.size();i++){
if(word.equals(arraySearch.get(i)))
return false;
}
return true ;
}
public static void Decompress(ArrayList<String>Dictionary, ArrayList<Integer>positionTag, ArrayList<String>nextCharTag){
String decompress="";
for(int i=0;i<positionTag.size();i++){
decompress=decompress+Dictionary.get(positionTag.get(i))+nextCharTag.get(i);
}
if(nextCharTag.get(positionTag.size()-1).equals("null"))
decompress=decompress.substring(0,decompress.length()-4);
System.out.println(decompress);
}
public static void main(String[] args) {
ArrayList<String> Dictionary = new ArrayList<>();
ArrayList<Integer> positionTag = new ArrayList<>();
ArrayList<String> nextCharTag = new ArrayList<>();
Dictionary.add("");
String inputWillCompressed,charInputAsString = null;
Scanner scannerInput=new Scanner(System.in);
inputWillCompressed=scannerInput.nextLine();
for (int i = 0;i< inputWillCompressed.length();i++ ) {
charInputAsString = Character.toString(inputWillCompressed.charAt(i));
if (StringNotFound(charInputAsString,Dictionary)) {
Dictionary.add(charInputAsString);
positionTag.add(0);
nextCharTag.add(charInputAsString);
}
else {
while (!StringNotFound(charInputAsString,Dictionary)) {
i++;
if(i==inputWillCompressed.length()){
charInputAsString = charInputAsString;
positionTag.add(indexOfStringInDic(charInputAsString, Dictionary));
nextCharTag.add("null");
break;
}
charInputAsString = charInputAsString + Character.toString(inputWillCompressed.charAt(i));
}
if(i!=inputWillCompressed.length()) {
Dictionary.add(charInputAsString);
positionTag.add(indexOfStringInDic(charInputAsString.substring(0, charInputAsString.length() - 1), Dictionary));
nextCharTag.add(Character.toString(inputWillCompressed.charAt(i)));
}
}
}
for(int i=0;i<positionTag.size();i++){
System.out.println("< "+positionTag.get(i)+" ,"+nextCharTag.get(i)+" >");
}
Decompress(Dictionary, positionTag, nextCharTag);
for(int i=0;i<Dictionary.size();i++){
System.out.println(Dictionary.get(i));
}
}
}
//abaababaababbbbbbbbbb