Java LZW解码未命中第一个代码条目
我跟踪了执行情况 我试着用我自己的字典而不是使用ASCII字典来进行LZW编码。 当我试着用我自己的词汇时,解码有一个问题。。。结果是错误的,因为每个解码的单词都不查看第一个字母。 结果必须是Java LZW解码未命中第一个代码条目,java,lzw,Java,Lzw,我跟踪了执行情况 我试着用我自己的字典而不是使用ASCII字典来进行LZW编码。 当我试着用我自己的词汇时,解码有一个问题。。。结果是错误的,因为每个解码的单词都不查看第一个字母。 结果必须是'abrac abra',而不是'braca brac bra' 我在String act=“”+(char)(int)compressed.remove(0)中看到decode()方法的问题这将删除所有第一个“a”字母。 但我不知道如何修改这行。。。 例如,如果我使用字符串act=“”而不是上面的行。。。
'abrac abra'
,而不是'braca brac bra'
我在String act=“”+(char)(int)compressed.remove(0)中看到decode()方法的问题代码>这将删除所有第一个“a”字母。
但我不知道如何修改这行。。。
例如,如果我使用字符串act=“”代码>而不是上面的行。。。编码将非常错误,或者使用另一个命令。。。我不知道怎么才能解决这个小问题。。。或者,也许我正在寻找解决问题的坏方法
public class LZW {
public static List<Integer> encode(String uncompressed) {
Map<String,Integer> dictionary = DictionaryInitStringInt();
int dictSize = dictionary.size();
String act = "";
List<Integer> result = new ArrayList<Integer>();
for (char c : uncompressed.toCharArray()) {
String next = act + c;
if (dictionary.containsKey(next))
act = next;
else {
result.add(dictionary.get(act));
// Add next to the dictionary.
dictionary.put(next, dictSize++);
act = "" + c;
}
}
// Output the code for act.
if (!act.equals(""))
result.add(dictionary.get(act));
return result;
}
public static String decode(List<Integer> compressed) {
Map<Integer,String> dictionary = DictionaryInitIntString();
int dictSize = dictionary.size();
String act = "" + (char)(int)compressed.remove(0);
//String act = "";
String result = act;
for (int k : compressed) {
String entry;
if (dictionary.containsKey(k))
entry = dictionary.get(k);
else if (k == dictSize)
entry = act + act.charAt(0);
else
throw new IllegalArgumentException("Nincs ilyen kulcs: " + k);
result += entry;
dictionary.put(dictSize++, act + entry.charAt(0));
act = entry;
}
return result;
}
public static Map<String,Integer> DictionaryInitStringInt()
{
char[] characters = {'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',' ','!',
'?','.',','};
int charactersLength = characters.length;
Map<String,Integer> dictionary = new HashMap<String,Integer>();
for (int i = 0; i < charactersLength; i++)
dictionary.put("" + characters[i], i);
return dictionary;
}
public static Map<Integer,String> DictionaryInitIntString()
{
char[] characters = {'a','b','c','d','e','f','g','h','i','j', 'k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',' ','!',
'?','.',','};
int charactersLength = characters.length;
Map<Integer,String> dictionary = new HashMap<Integer,String>();
for (int i = 0; i < charactersLength; i++)
dictionary.put(i,"" + characters[i]);
return dictionary;
}
public static void main(String[] args) {
List<Integer> compressed = encode("abraca abrac abra");
System.out.println(compressed);
String decodeed = decode(compressed);
// decodeed will be 'braca brac bra'
System.out.println(decodeed);
}
公共类LZW{
公共静态列表编码(字符串未压缩){
Map dictionary=DictionaryInitStringInt();
int dictSize=dictionary.size();
字符串act=“”;
列表结果=新建ArrayList();
for(char c:uncompressed.toCharArray()){
字符串next=act+c;
if(dictionary.containsKey(下一个))
act=下一步;
否则{
result.add(dictionary.get(act));
//添加到字典旁边。
dictionary.put(下一步,dictSize++);
act=”“+c;
}
}
//输出act的代码。
如果(!act.equals(“”)
result.add(dictionary.get(act));
返回结果;
}
公共静态字符串解码(列表压缩){
Map dictionary=dictionaryinitstring();
int dictSize=dictionary.size();
字符串act=”“+(char)(int)已压缩。删除(0);
//字符串act=“”;
字符串结果=act;
对于(int k:compressed){
字符串输入;
if(字典containsKey(k))
entry=dictionary.get(k);
else if(k==dictSize)
条目=act+act.charAt(0);
其他的
抛出新的IllegalArgumentException(“Nincs ilyen kulcs:+k”);
结果+=输入;
dictionary.put(dictSize++,act+entry.charAt(0));
act=进入;
}
返回结果;
}
公共静态映射字典yinitstringint()
{
字符[]字符={a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'n',
‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’、‘w’、‘x’、‘y’、‘z’、‘’、‘’、‘!’,
'?','.',','};
int charactersLength=characters.length;
Map dictionary=newhashmap();
for(int i=0;i
}罗塞塔示例使用
"" + (char) (int) compressed.remove(0);
因为字典映射的前256个条目正好是“char”值
如果使用自定义措辞,则此行应为:
String act = dictionary.get(compressed.remove(0));