LZW算法 golang实现
来源:互联网 发布:新日铁住金软件上海 编辑:程序博客网 时间:2024/05/20 01:39
// LZW Data Compression in Golangpackage mainimport "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)}
参考
http://www.golangprograms.com/golang-program-for-implementation-lzw-data-compression-and-uncompression.html
阅读全文
0 0
- LZW算法 golang实现
- LZW算法的 C#实现
- LZW算法PHP实现方法
- lzw字符串压缩算法实现
- LZW算法
- lzw压缩算法的c语言实现
- LZW for GIF 算法原理和实现
- LZW算法压缩c语言实现
- LZW算法压缩c语言实现
- java实现的LZW 压缩算法源码
- lzw压缩算法的c语言实现
- 用python实现的LZW算法
- LZW for GIF 算法原理和实现
- LZW for GIF 算法原理和实现
- LZW编解码算法(C实现)
- LZW 压缩算法的C++实现
- LZW编解码算法实现与分析_C语言实现
- 排序算法 golang 实现
- 事务
- 大家设计数据库时是否使用外键
- Spring Boot 读取配置文件的几种方式
- leetcode.394 Decode String
- javascript作用域与作用域链、闭包、对象创建模式、继承模式
- LZW算法 golang实现
- CentOS 6.5 中 Yum 方法安装 MySQL 5.7 完整步骤
- C语言位运算符:与、或、异或、取反、左移与右移详细介绍
- toast和menu的使用
- Hibernate与mybatis的区别
- 贪心算法总结
- 信息论实验-通信系统仿真(C++)
- 6位36位进制转十进制加一,十进制转36位进制
- hdu3507 斜率dp