用Python 和 Java 写的Sunday字符串排序算法
来源:互联网 发布:java基础教程 pdf下载 编辑:程序博客网 时间:2024/06/09 16:57
Python
import re# 采用Sunday算法的字符串匹配# 输入:模式串,匹配串# 输出:所有匹配模式的首字符地址def findBob(mode, match): positions = [] # 返回值 len_mode = len(mode) len_match = len(match) endIndex = len_match - len_mode + 1 for i in range(0, endIndex): for j in range(0, len_mode): if match[i + j] == mode[j]: if j == (len_mode - 1): positions.append(i) break else: t = i + len_mode if t >= len_match: break if match[t] in mode: array = [m.start() for m in re.finditer(match[t], mode)] move = len_mode - array[len(array) - 1] i = i + move - 1 else: i = i + t - 1 break return positions# 主函数key_word = 'bob'string = "azcbobobegghakl"positions = findBob(key_word, string)print("Number of times bob occurs is:" + str(len(positions)))print(positions)
Java
为了方便看过程,把过程也输出了
package zhp.sunday;import java.util.ArrayList;public class Sunday {public static void main(String[] args) {String mode = "bob";String match = "azcbobobegghakl";ArrayList<Integer> positions = findSubstring(mode, match);System.out.println(positions);}/** * @param mode * 模式串 * @param match * 匹配串 */public static ArrayList<Integer> findSubstring(String mode, String match) {ArrayList<Integer> positions = new ArrayList<>();int len_mode = mode.length();int len_match = match.length();int endIndex = len_match - len_mode + 1;for (int i = 0; i < endIndex; i++) { // 遍历匹配串System.out.println("======================");for (int j = 0; j < len_mode; j++) { // 遍历模式串System.out.println("i = " + i + ", j = " + j);System.out.println("检查match中 " + match.charAt(i + j) + " , 和mode中 "+ mode.charAt(j));if (match.charAt(i + j) == mode.charAt(j)) { // 如果在j这个位置上匹配到了System.out.println(" 这两个字符相同");if (j == (len_mode - 1)) { // 如果 j 这个位置是模式串的最后一位,说明匹配成功System.out.println(" 匹配到最后一位,匹配成功!");positions.add(i);break;}} else { // 如果在j这个位置上没有匹配到System.out.println(" 这两个字符不同");int t = i + len_mode; // 模式串后面那一位if (t >= len_match) {System.out.println(" 找到最后一位的下一位:t = " + t + ", 超出了范围");break;}System.out.println(" 找到最后一位的下一位:t = " + t + ", match[t] = " + match.charAt(t));if (mode.contains("" + match.charAt(t))) {System.out.println(" 模式串中包含这个字符:" + match.charAt(t));int index = mode.lastIndexOf("" + match.charAt(t)); System.out.println(" 该字符在模式串中的位置是:" + index);int move = len_mode - index;System.out.println(" 模式串应该往右移动 " + move + "位。");System.out.println(" 旧的i = " + i);i = i + move - 1;System.out.println(" 新的i = " + i);} else {System.out.println(" 模式串中不包含这个字符:" + match.charAt(t));System.out.println(" 旧的i = " + i);i = i + t - 1;System.out.println(" 新的i = " + i);if (i >= endIndex) {System.out.println(" 新的i越界, break;");}}break;}}}return positions;}}
0 0
- 用Python 和 Java 写的Sunday字符串排序算法
- 字符串匹配算法(KMP、BM和Sunday),及Python实现
- 字符串匹配算法(KMP、BM和Sunday),及Python实现
- sunday算法和自己关于字符串匹配的一些思考
- Sunday字符串匹配算法,java实现
- 字符串匹配sunday算法java实现
- 字符串匹配:kmp,bm和sunday算法
- 字符串匹配算法——Sunday算法和KMP算法(java版本)
- 字符串查找算法Sunday的实现
- 字符串的模式匹配:Sunday 算法
- 用sunday算法解决字符串匹配问题
- 字符串匹配---Sunday算法
- 字符串匹配Sunday算法
- 字符串匹配-sunday算法
- 字符串匹配--Sunday算法
- 字符串匹配 sunday算法
- 字符串匹配sunday算法
- Sunday 字符串匹配算法
- WEB程序设计之DOM
- 2014-10-31Android学习------SQLite数据库操作(四)-----数据库模块(二)增删改查
- 面向对象程序设计上机练习九(对象指针)
- Android学习笔记2——android的四大组件
- codeblocks 背景设置和光标的设置!!!
- 用Python 和 Java 写的Sunday字符串排序算法
- UVA - 10905 Children's Game 字符串的排序
- 基数排序
- Java compiler level does not match the version of the installed Java project facet. 错误处理
- LeetCode[Array]: Pascal's Triangle
- 会话bean综述(未排版)
- TNS-12571: TNS: 包写入程序失败
- 开始写博客了
- 面试题目——基础篇