用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
原创粉丝点击