数据结构课程实践(3)-代码补充 BF算法(朴素匹配算法)

数据结构课程实践(3)-代码补充

—BF算法(朴素匹配算法)

算法思想:

设主串搜索起始位置为i
通过主串和子串一一对比
不满足时,子串回到初始位置,主串回到初始位置加一处
满足时,主串变为i+子串.length()-1处,子串回初始位置
继续开始匹配后面的字符
直到i>=主串.length()停止。

之后就是输出结果

代码实现:

1.运行类

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Action {
    String xml;
    String word;
    Scanner scanner =new Scanner(System.in);
    public void readTXT(){
        try{
            StringBuffer buffer = new StringBuffer();
            BufferedReader bf= new BufferedReader(new FileReader("D:\\学生成绩管理系统数据文件\\article.txt"));
            String s = null;
            while((s = bf.readLine())!=null){
                buffer.append(s.trim());
            }
            xml = buffer.toString();
            System.out.println(xml);
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
    }
    public void start(){
        int count;
        readTXT();
        System.out.println("请输入要查询的单词");
        word=scanner.next();
        count=bF(xml,word);
        if(count==0){
            System.out.println("未找到单词");
        }
        else{
            System.out.println("一共找到"+count+"个");
        }
    }
    public int bF(String a,String b){
        int num=0;
        int i=0;
        int j=0;//子串移动
        int t;//记录主串的开始位置
        char []a1=a.toCharArray();
        char []b1=b.toCharArray();
        while(i<=a1.length){
            t=i;
            for(j=0;j<b1.length&&i<a1.length;j++)
            {
                if(a1[i]!=b1[j])
                {
                    break;
                }
                else{
                    i++;
                }
            }
            if((i-t)>=b1.length){
                location(t,b);
                num++;
                i=t+b1.length-1;
            }
            else {
                i=t+1;
            }
        }
        return num;
    }
    public void location(int x,String word){
        int t=x+word.length()-1;
        System.out.println("单词位置:"+x+"~"+t);
    }
}

2.主类

public class Main {
    public static void main(String[]args){
        Action action=new Action();
        action.start();
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
BF算法和KMP算法都是串的模式匹配算法,但是它们的时间复杂度不同。BF算法的时间复杂度为O(m*n),其中m和n分别为主串和模式串的长度。而KMP算法的时间复杂度为O(m+n)。因此,当模式串较长时,KMP算法的效率更高。 下面是BF算法和KMP算法的介绍和演示: 1. BF算法(暴力匹配算法BF算法是一种朴素的模式匹配算法,它的思想是从主串的第一个字符开始,依次和模式串的每个字符进行比较,如果匹配成功,则继续比较下一个字符,否则从主串的下一个字符开始重新匹配BF算法的时间复杂度为O(m*n)。 下面是BF算法的Python代码演示: ```python def BF(main_str, pattern_str): m = len(main_str) n = len(pattern_str) for i in range(m-n+1): j = 0 while j < n and main_str[i+j] == pattern_str[j]: j += 1 if j == n: return i return -1 # 测试 main_str = 'ababcabcacbab' pattern_str = 'abcac' print(BF(main_str, pattern_str)) # 输出:6 ``` 2. KMP算法(Knuth-Morris-Pratt算法) KMP算法是一种改进的模式匹配算法,它的核心思想是利用已经匹配过的信息,尽量减少模式串与主串的匹配次数。具体来说,KMP算法通过预处理模式串,得到一个next数组,用于指导匹配过程中的跳转。KMP算法的时间复杂度为O(m+n)。 下面是KMP算法的Python代码演示: ```python def KMP(main_str, pattern_str): m = len(main_str) n = len(pattern_str) next = getNext(pattern_str) i = 0 j = 0 while i < m and j < n: if j == -1 or main_str[i] == pattern_str[j]: i += 1 j += 1 else: j = next[j] if j == n: return i - j else: return -1 def getNext(pattern_str): n = len(pattern_str) next = [-1] * n i = 0 j = -1 while i < n-1: if j == -1 or pattern_str[i] == pattern_str[j]: i += 1 j += 1 next[i] = j else: j = next[j] return next # 测试 main_str = 'ababcabcacbab' pattern_str = 'abcac' print(KMP(main_str, pattern_str)) # 输出:6 ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nightelves11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值