BruteForceStringMatching
来源:互联网 发布:如何制作精美ppt 知乎 编辑:程序博客网 时间:2024/05/14 13:14
//Recall the string-matching problem introduced before: given a string of//n characters called text and a string of m characters(m <= n) called the//pattern, find a substring of the text that matches the pattern. To put//it more precisely, we want to find the i - the index of the leftmost character//of the first matching substring in the text.//If matches other than the first one need to be found, a string-matching algorithm//can simply continue working until the entire text is exhausted.//A brute-force algorithm for the string-matching problem is quite obvious://align the pattern against the first m characters of the text and start matching//the corresponding pairs of characters from left to right until either all the//m pairs of the characters match(then the algorithm can stop) or a mismatching//pair is encountered. In the latter case, shift the patter on position to the//right and resume the character comparisons, starting again with the first//character of the pattern and its counterpart in the text. Note that the last//position in the text that can still be a beginning of a matching substring//is n - m(provided the text positions are indexed from 0 to n-1). Beyond that//position, there are not enough characters to match the entire pattern; hence,//the algorithm need not make any comparisons there.package mainimport ( "fmt")func BruteForceStringMatching(text, pattern []rune) int { n := len(text) m := len(pattern) for i := 0; i <= n-m; i++ { for j := 0; j < m && pattern[j] == text[i+j]; { j++ if j == m { return i } } } return -1}func main() { text := "暴力法解决字符串匹配问题,字符串匹配问题用暴力法" pattern := "暴力" fmt.Println(BruteForceStringMatching([]rune(text), []rune(pattern))) text2 := []rune("abedefgabdedsdfsesdffses") patter2 := []rune("ses") fmt.Println(BruteForceStringMatching(text2, patter2))}
0 0
- BruteForceStringMatching
- 40. PHP if
- ZOJ:2672 Fibonacci Subsequence(动态规划+hash)
- JQuery在轮循中取得当前fieldset中指定的元素值
- 山东省第四届ACM C :A^X mod P
- Android 与 web服务器Servlet联调
- BruteForceStringMatching
- [Egret学习笔记 四]声音的使用
- 41. PHP else
- Access,Hybrid和Trunk
- onCreate中的savedInstanceState有何具体作用
- 64位 CentOS6.6+7.1 配置Hadoop-2.6.0集群
- Win7+VS+NVIDIA+OpenCL安装
- 查看iOS设备UDID
- 黑马程序员—java语法部分总结