- 数据结构与算法
- DSA - 首页
- DSA - 概述
- DSA - 环境设置
- DSA - 算法基础
- DSA - 渐近分析
- 数据结构
- DSA - 数据结构基础
- DSA - 数据结构和类型
- DSA - 数组数据结构
- 链表
- DSA - 链表数据结构
- DSA - 双向链表数据结构
- DSA - 循环链表数据结构
- 栈与队列
- DSA - 栈数据结构
- DSA - 表达式解析
- DSA - 队列数据结构
- 搜索算法
- DSA - 搜索算法
- DSA - 线性搜索算法
- DSA - 二分搜索算法
- DSA - 插值搜索
- DSA - 跳跃搜索算法
- DSA - 指数搜索
- DSA - 斐波那契搜索
- DSA - 子列表搜索
- DSA - 哈希表
- 排序算法
- DSA - 排序算法
- DSA - 冒泡排序算法
- DSA - 插入排序算法
- DSA - 选择排序算法
- DSA - 归并排序算法
- DSA - 希尔排序算法
- DSA - 堆排序
- DSA - 桶排序算法
- DSA - 计数排序算法
- DSA - 基数排序算法
- DSA - 快速排序算法
- 图数据结构
- DSA - 图数据结构
- DSA - 深度优先遍历
- DSA - 广度优先遍历
- DSA - 生成树
- 树数据结构
- DSA - 树数据结构
- DSA - 树的遍历
- DSA - 二叉搜索树
- DSA - AVL树
- DSA - 红黑树
- DSA - B树
- DSA - B+树
- DSA - 伸展树
- DSA - 字典树
- DSA - 堆数据结构
- 递归
- DSA - 递归算法
- DSA - 使用递归实现汉诺塔
- DSA - 使用递归实现斐波那契数列
- 分治法
- DSA - 分治法
- DSA - 最大最小问题
- DSA - Strassen矩阵乘法
- DSA - Karatsuba算法
- 贪心算法
- DSA - 贪心算法
- DSA - 旅行商问题(贪心法)
- DSA - Prim最小生成树
- DSA - Kruskal最小生成树
- DSA - Dijkstra最短路径算法
- DSA - 地图着色算法
- DSA - 分数背包问题
- DSA - 带截止日期的作业排序
- DSA - 最优合并模式算法
- 动态规划
- DSA - 动态规划
- DSA - 矩阵链乘法
- DSA - Floyd-Warshall算法
- DSA - 0-1背包问题
- DSA - 最长公共子序列算法
- DSA - 旅行商问题(动态规划法)
- 近似算法
- DSA - 近似算法
- DSA - 顶点覆盖算法
- DSA - 集合覆盖问题
- DSA - 旅行商问题(近似算法)
- 随机化算法
- DSA - 随机化算法
- DSA - 随机化快速排序算法
- DSA - Karger最小割算法
- DSA - Fisher-Yates洗牌算法
- DSA有用资源
- DSA - 问答
- DSA - 快速指南
- DSA - 有用资源
- DSA - 讨论
朴素模式匹配算法
数据结构中的朴素模式匹配算法
朴素模式匹配是各种模式匹配算法中最简单的一种方法。虽然它比暴力方法更有效率,但它并不是最优的方法。与暴力法一样,它也依次检查主字符串的所有字符以查找模式。因此,其时间复杂度为O(m*n),其中'm'是模式的大小,'n'是主字符串的大小。此算法仅适用于较小的文本。
朴素模式匹配算法不需要任何预处理阶段。我们可以通过一次检查字符串来找到子字符串。它也不占用额外的空间来执行操作。如果找到匹配项,则模式匹配操作的最终结果将是指定模式的索引,否则为-1。此外,如果所需模式在主字符串中多次出现,此操作可以返回所有索引。
让我们通过一个例子来了解模式匹配问题的输入输出场景:
Input: main String: "ABAAABCDBBABCDDEBCABC" pattern: "ABC" Output: Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18
示例
在下面的示例中,我们将演示如何应用朴素方法来解决模式匹配问题。
#include<stdio.h>
#include<string.h>
// method to search for pattern
void naiveFindPatrn(char* mainString, char* pattern, int array[], int *index) {
int patLen = strlen(pattern);
int strLen = strlen(mainString);
// outer for loop
for(int i = 0; i<=(strLen - patLen); i++) {
int j;
// to check for each character of pattern
for(j = 0; j<patLen; j++) {
if(mainString[i+j] != pattern[j])
break;
}
// to print the index of the pattern is found
if(j == patLen) {
(*index)++;
array[(*index)] = i;
}
}
}
// main method starts
int main() {
// main string
char mainString[] = "ABAAABCDBBABCDDEBCABC";
// pattern to be found
char pattern[] = "ABC";
int locArray[strlen(mainString)];
int index = -1;
naiveFindPatrn(mainString, pattern, locArray, &index);
// to print the indices
for(int i = 0; i <= index; i++) {
printf("Pattern found at position: %d\n", locArray[i]);
}
return 0;
}
#include<iostream>
using namespace std;
// method to search for pattern
void naiveFindPatrn(string mainString, string pattern, int array[], int *index) {
int patLen = pattern.size();
int strLen = mainString.size();
// outer for loop
for(int i = 0; i<=(strLen - patLen); i++) {
int j;
// to check for each character of pattern
for(j = 0; j<patLen; j++) {
if(mainString[i+j] != pattern[j])
break;
}
// to print the index of the pattern is found
if(j == patLen) {
(*index)++;
array[(*index)] = i;
}
}
}
// main method starts
int main() {
// main string
string mainString = "ABAAABCDBBABCDDEBCABC";
// pattern to be found
string pattern = "ABC";
int locArray[mainString.size()];
int index = -1;
naiveFindPatrn(mainString, pattern, locArray, &index);
// to print the indices
for(int i = 0; i <= index; i++) {
cout << "Pattern found at position: " << locArray[i]<<endl;
}
}
public class Main {
// method to search for pattern
static void naiveFindPatrn(String mainString, String pattern, int[] array) {
int patLen = pattern.length();
int strLen = mainString.length();
int index = 0;
// outer for loop
for(int i = 0; i <= (strLen - patLen); i++) {
int j;
// to check for each character of pattern
for(j = 0; j < patLen; j++) {
if(mainString.charAt(i+j) != pattern.charAt(j))
break;
}
// to print the index of the pattern is found
if(j == patLen) {
array[index] = i;
index++;
}
}
}
// main method starts
public static void main(String[] args) {
// main string
String mainString = "ABAAABCDBBABCDDEBCABC";
// pattern to be found
String pattern = "ABC";
int[] locArray = new int[mainString.length()];
naiveFindPatrn(mainString, pattern, locArray);
// to print the indices
for(int i = 0; i < locArray.length && locArray[i] != 0; i++) {
System.out.println("Pattern found at position: " + locArray[i]);
}
}
}
# method to search for pattern
def naiveFindPatrn(mainString, pattern):
patLen = len(pattern)
strLen = len(mainString)
indices = []
# outer for loop
for i in range(strLen - patLen + 1):
j = 0
# to check for each character of pattern
for j in range(patLen):
if mainString[i+j] != pattern[j]:
break
# to print the index of the pattern is found
if j == patLen - 1 and mainString[i+j] == pattern[j]:
indices.append(i)
return indices
# main method starts
if __name__ == "__main__":
# main string
mainString = "ABAAABCDBBABCDDEBCABC"
# pattern to be found
pattern = "ABC"
indices = naiveFindPatrn(mainString, pattern)
# to print the indices
for i in indices:
print("Pattern found at position:", i)
输出
Pattern found at position: 4 Pattern found at position: 10 Pattern found at position: 18
广告