实现代码:
package main
import "fmt"
func compressLZW(testStr string) []int {
code := 256
dictionary := make(map[string]int)
for i := 0; i < 256; i++ {
dictionary[string(i)] = i
}
currChar := ""
result := make([]int, 0)
for _, c := range []byte(testStr) {
phrase := currChar + string(c)
if _, isTrue := dictionary[phrase]; isTrue {
currChar = phrase
} else {
result = append(result, dictionary[currChar])
dictionary[phrase] = code
code++
currChar = string(c)
}
}
if currChar != "" {
result = append(result, dictionary[currChar])
}
return result
}
func decompressLZW(compressed []int) string {
code := 256
dictionary := make(map[int]string)
for i := 0; i < 256; i++ {
dictionary[i] = string(i)
}
currChar := string(compressed[0])
result := currChar
for _, element := range compressed[1:] {
var word string
if x, ok := dictionary[element]; ok {
word = x
} else if element == code {
word = currChar + currChar[:1]
} else {
panic(fmt.Sprintf("Bad compressed element: %d", element))
}
result += word
dictionary[code] = currChar + word[:1]
code++
currChar = word
}
return result
}
func main() {
fmt.Print("Enter any string :")
var testStr string
fmt.Scanln(&testStr)
compressed := compressLZW(testStr)
fmt.Println("\nAfter Compression :", compressed)
uncompression := decompressLZW(compressed)
fmt.Println("\nAfter Uncompression :", uncompression)
}
输出:
Enter any string :Australia
After Compression : [65 117 115 116 114 97 108 105 97]
After Uncompression : Australia
C:\golang\example>go run test.go
Enter any string :Germany
After Compression : [71 101 114 109 97 110 121]
After Uncompression : Germany