问题:我们在这里讨论了朴素字符串匹配算法。考虑一个模式的所有字符都不同的情况。我们可以修改原始的Naive String Matching算法,使其对这些类型的模式更好地工作吗?如果可以的话,原始算法会有什么变化?
解决方案:在原始的Naive String匹配算法中,我们总是将模式滑动1。当模式的所有字符都不同时,我们可以将模式滑动1以上。让我们看看如何做到这一点。当j个匹配后发生不匹配时,我们知道pattern的第一个字符将不匹配j个匹配的字符,因为pattern的所有字符都是不同的。因此,我们始终可以将模式滑动j而不丢失任何有效的移位。以下是针对特殊模式进行了优化的修改后的代码。
C++
/* C++ program for A modified Naive Pattern Searching
algorithm that is optimized for the cases when all
characters of pattern are different */
#include
using namespace std;
/* A modified Naive Pettern Searching
algorithn that is optimized for the
cases when all characters of pattern are different */
void search(string pat, string txt)
{
int M = pat.size();
int N = txt.size();
int i = 0;
while (i <= N - M)
{
int j;
/* For current index i, check for pattern match */
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
{
cout << "Pattern found at index " << i << endl;
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j; // slide the pattern by j
}
}
/* Driver code*/
int main()
{
string txt = "ABCEABCDABCEABCD";
string pat = "ABCD";
search(pat, txt);
return 0;
}
// This code is contributed by rathbhupendra
C
/* C program for A modified Naive Pattern Searching
algorithm that is optimized for the cases when all
characters of pattern are different */
#include
#include
/* A modified Naive Pettern Searching algorithn that is optimized
for the cases when all characters of pattern are different */
void search(char pat[], char txt[])
{
int M = strlen(pat);
int N = strlen(txt);
int i = 0;
while (i <= N - M)
{
int j;
/* For current index i, check for pattern match */
for (j = 0; j < M; j++)
if (txt[i+j] != pat[j])
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
{
printf("Pattern found at index %d \n", i);
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j; // slide the pattern by j
}
}
/* Driver program to test above function */
int main()
{
char txt[] = "ABCEABCDABCEABCD";
char pat[] = "ABCD";
search(pat, txt);
return 0;
}
Java
/* Java program for A modified Naive Pattern Searching
algorithm that is optimized for the cases when all
characters of pattern are different */
class GFG
{
/* A modified Naive Pettern Searching
algorithn that is optimized for the
cases when all characters of pattern are different */
static void search(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
int i = 0;
while (i <= N - M)
{
int j;
/* For current index i, check for pattern match */
for (j = 0; j < M; j++)
if (txt.charAt(i + j) != pat.charAt(j))
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
{
System.out.println("Pattern found at index "+i);
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j; // slide the pattern by j
}
}
/* Driver code*/
public static void main (String[] args)
{
String txt = "ABCEABCDABCEABCD";
String pat = "ABCD";
search(pat, txt);
}
}
// This code is contributed by chandan_jnu
Python
# Python program for A modified Naive Pattern Searching
# algorithm that is optimized for the cases when all
# characters of pattern are different
def search(pat, txt):
M = len(pat)
N = len(txt)
i = 0
while i <= N-M:
# For current index i, check for pattern match
for j in xrange(M):
if txt[i+j] != pat[j]:
break
j += 1
if j==M: # if pat[0...M-1] = txt[i,i+1,...i+M-1]
print "Pattern found at index " + str(i)
i = i + M
elif j==0:
i = i + 1
else:
i = i+ j # slide the pattern by j
# Driver program to test the above function
txt = "ABCEABCDABCEABCD"
pat = "ABCD"
search(pat, txt)
# This code is contributed by Bhavya Jain
C#
/* C# program for A modified Naive Pattern Searching
algorithm that is optimized for the cases when all
characters of pattern are different */
using System;
class GFG
{
/* A modified Naive Pettern Searching
algorithn that is optimized for the
cases when all characters of pattern are different */
static void search(string pat, string txt)
{
int M = pat.Length;
int N = txt.Length;
int i = 0;
while (i <= N - M)
{
int j;
/* For current index i, check for pattern match */
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
{
Console.WriteLine("Pattern found at index "+i);
i = i + M;
}
else if (j == 0)
i = i + 1;
else
i = i + j; // slide the pattern by j
}
}
/* Driver code*/
static void Main()
{
string txt = "ABCEABCDABCEABCD";
string pat = "ABCD";
search(pat, txt);
}
}
// This code is contributed by chandan_jnu
PHP
输出:
Pattern found at index 4
Pattern found at index 12