feat(Go-Tool):2021/03/11: 新增Kr、Shift and/or字符串匹配算法
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 65 KiB |
@@ -72,6 +72,8 @@
|
|||||||
|
|
||||||
2021/03/02: 新增字符串匹配 暴力匹配、KMP匹配、SunDay匹配算法
|
2021/03/02: 新增字符串匹配 暴力匹配、KMP匹配、SunDay匹配算法
|
||||||
|
|
||||||
|
2021/03/11: 新增Kr、Shift and/or字符串匹配算法
|
||||||
|
|
||||||
# 修复日志
|
# 修复日志
|
||||||
2020/11/23:修改项目案例(按照一定规则对一组数据进行排序分组)
|
2020/11/23:修改项目案例(按照一定规则对一组数据进行排序分组)
|
||||||
|
|
||||||
@@ -102,3 +104,5 @@
|
|||||||
- 通过 go mod vendor 切换到vendor管理
|
- 通过 go mod vendor 切换到vendor管理
|
||||||
- 在文件中import引用对应的路径
|
- 在文件中import引用对应的路径
|
||||||
- vendor加载对应的包
|
- vendor加载对应的包
|
||||||
|
|
||||||
|

|
||||||
@@ -14,10 +14,10 @@ import (
|
|||||||
func TestHorspool(t *testing.T) {
|
func TestHorspool(t *testing.T) {
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "abcd")) //输出0
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "abcd")) //输出0
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "bcd")) //输出1
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "bcd")) //输出1
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "fgh")) //输出6
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "fgh")) //输出5
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "hijk")) //输出8
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "hijk")) //输出7
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "jk")) //输出10
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "jk")) //输出9
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "k")) //输出11
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "k")) //输出10
|
||||||
|
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "kbz")) //输出-1
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "kbz")) //输出-1
|
||||||
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "kdasdsad")) //输出-1
|
fmt.Println("该子串的位置为:", Horspool("abcdefghijk", "kdasdsad")) //输出-1
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/3/5 13:17
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package stringMatch
|
||||||
|
|
||||||
|
func KarpRabinMatch(allString, modeString string) int {
|
||||||
|
hashMode := hash(modeString)
|
||||||
|
for i := 0; i < len(allString)-len(modeString)+1; i++ {
|
||||||
|
hashKey := hash(allString[i : i+len(modeString)+1])
|
||||||
|
if hashMode == hashKey {
|
||||||
|
for j := 0; j < len(modeString); j++ {
|
||||||
|
if allString[i+j] != modeString[j] {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func hash(s string) int {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/3/5 13:25
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package stringMatch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestKarpRabinMatch(t *testing.T) {
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "abcd")) //输出0
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "bcd")) //输出1
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "fgh")) //输出5
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "hijk")) //输出7
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "jk")) //输出9
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "k")) //输出10
|
||||||
|
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "kbz")) //输出-1
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "kdasdsad")) //输出-1
|
||||||
|
fmt.Println("该子串的位置为:", KarpRabinMatch("abcdefghijk", "dsadsa")) //输出-1
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/3/8 14:42
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package stringMatch
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func ShiftAndMatch(allString, modeString string) int {
|
||||||
|
if len(modeString) > 32 {
|
||||||
|
panic("暂只支持32位大小")
|
||||||
|
}
|
||||||
|
|
||||||
|
bitap := GenerateTable(modeString)
|
||||||
|
|
||||||
|
fmt.Println("bitap的二进制:")
|
||||||
|
for i := 0; i < len(modeString); i++ {
|
||||||
|
fmt.Println(fmt.Sprintf("当前字符:%c,对应二进制:%08b", modeString[i], bitap[modeString[i]-'a']))
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("")
|
||||||
|
|
||||||
|
var status int
|
||||||
|
for i := 0; i < len(allString); i++ {
|
||||||
|
status = ((status << 1) | 1) & bitap[allString[i]-'a']
|
||||||
|
fmt.Println(fmt.Sprintf("当前字符:%c,对应的二进制结果:%08b", allString[i], status))
|
||||||
|
if status&(1<<(len(modeString)-1)) > 0 {
|
||||||
|
return i - len(modeString) + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func GenerateTable(modeString string) []int {
|
||||||
|
bitap := make([]int, 32)
|
||||||
|
for i := 0; i < len(modeString); i++ {
|
||||||
|
bitap[modeString[i]-'a'] |= 1 << i
|
||||||
|
}
|
||||||
|
return bitap
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShiftOrMatch(allString, modeString string) int {
|
||||||
|
if len(modeString) > 32 {
|
||||||
|
panic("暂只支持32位大小")
|
||||||
|
}
|
||||||
|
|
||||||
|
bitap := make([]int, 32)
|
||||||
|
for i := 0; i < 32; i++ {
|
||||||
|
bitap[i] = ^0
|
||||||
|
}
|
||||||
|
|
||||||
|
var shift = 1
|
||||||
|
for i := 0; i < len(modeString); i++ {
|
||||||
|
bitap[modeString[i]-'a'] &= ^shift
|
||||||
|
shift <<= 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("bitap的二进制:")
|
||||||
|
for i := 0; i < len(modeString); i++ {
|
||||||
|
fmt.Println(fmt.Sprintf("当前字符:%c,对应二进制:%08b", modeString[i], uint32(bitap[modeString[i]-'a'])))
|
||||||
|
}
|
||||||
|
fmt.Println()
|
||||||
|
|
||||||
|
status := ^0
|
||||||
|
mask := ^(1 << (len(modeString) - 1))
|
||||||
|
fmt.Println(fmt.Sprintf("mask:%08b", uint32(mask)))
|
||||||
|
for i := 0; i < len(allString); i++ {
|
||||||
|
status = (status << 1) | bitap[allString[i]-'a']
|
||||||
|
fmt.Println(fmt.Sprintf("当前字符:%c,对应的二进制结果:%08b", allString[i], uint32(status)))
|
||||||
|
if ^(status | mask) > 0 {
|
||||||
|
return i - len(modeString) + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* @Author : huangzj
|
||||||
|
* @Time : 2021/3/8 14:51
|
||||||
|
* @Description:
|
||||||
|
*/
|
||||||
|
|
||||||
|
package stringMatch
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestShiftAndMatch(t *testing.T) {
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "abcd")) //输出0
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "bcd")) //输出1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "fgh")) //输出5
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "hijk")) //输出7
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "jk")) //输出9
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "k")) //输出10
|
||||||
|
//
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "kbz")) //输出-1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "kdasdsad")) //输出-1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftAndMatch("abcdefghijk", "dsadsa")) //输出-1
|
||||||
|
|
||||||
|
fmt.Println("该子串的位置为:", ShiftAndMatch("cbcbcbaefd", "cbcba"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestShiftOrMatch(t *testing.T) {
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "abcd")) //输出0
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "bcd")) //输出1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "fgh")) //输出5
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "hijk")) //输出7
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "jk")) //输出9
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "k")) //输出10
|
||||||
|
//
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "kbz")) //输出-1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "kdasdsad")) //输出-1
|
||||||
|
//fmt.Println("该子串的位置为:", ShiftOrMatch("abcdefghijk", "dsadsa")) //输出-1
|
||||||
|
|
||||||
|
fmt.Println("该子串的位置为:", ShiftOrMatch("cbcbcbaefd", "cbcba"))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user