الگوریتم ساده بهینه برای جستجوی الگو — راهنمای کاربردی
پیشتر، در مطلب «جستجوی الگو (Pattern Searching) — به زبان ساده» یک الگوریتم تطبیق رشته ساده مورد بررسی قرار گرفت. اکنون، شرایطی در نظر گرفته میشود که در آن، همه کاراکترهای الگو متفاوت هستند. پرسشی که در این وهله مطرح می شود این است که آیا میتوان الگوریتم ساده ارائه شده برای جستجوی الگو را به نوعی ویرایش کرد تا برای این نوع از الگوها نیز عملکرد بهتری داشته باشد؟ اگر امکان این امر وجود دارد، چه تغییراتی باید در الگوریتم اصلی داده شود. در ادامه، الگوریتم ساده بهینه برای جستجوی الگو مورد بررسی قرار میگیرد و پیادهسازی آن در زبانهای برنامهنویسی گوناگون شامل ++C، پایتون، C، جاوا، #C و PHP انجام میشود.
الگوریتم ساده بهینه شده برای جستجوی الگو
در الگوریتم تطبیق رشته نایو، همیشه الگو ۱ واحد اسلاید میشود. هنگامی که همه کاراکترهای الگو متفاوت باشند، میتوان الگو را بیش از ۱ واحد اسلاید کرد. در ادامه، روش انجام این کار مورد بررسی قرار میگیرد. هنگامی که یک عدم تطابق بعد از تطبیق پیدا کردن j اتفاق میافتد، اولین کاراکتر الگو با کاراکترهای تطبیق پیدا کرده با j تطبیق پیدا نمیکند؛ زیرا همه کاراکترهای الگو متفاوت هستند.
بنابراین، همیشه میتوان الگو را با j و بدون از دست دادن هر شیف معتبری، اسلاید کرد. در ادامه، کد پیادهسازی الگوریتم نایو بهینهسازی شده برای جستجوی الگو ارائه شده است. شایان توجه است که منظور از اسلاید کردن، در واقع داشتن یک کشوی لغزنده فرضی است که روی کاراکترها جا به جا میشود.
الگوریتم ساده بهینه برای جستجوی الگو در ++C
1/* C++ program for A modified Naive Pattern Searching
2algorithm that is optimized for the cases when all
3characters of pattern are different */
4#include <bits/stdc++.h>
5using namespace std;
6
7/* A modified Naive Pettern Searching
8algorithn that is optimized for the
9cases when all characters of pattern are different */
10void search(string pat, string txt)
11{
12 int M = pat.size();
13 int N = txt.size();
14 int i = 0;
15
16 while (i <= N - M)
17 {
18 int j;
19
20 /* For current index i, check for pattern match */
21 for (j = 0; j < M; j++)
22 if (txt[i + j] != pat[j])
23 break;
24
25 if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
26 {
27 cout << "Pattern found at index " << i << endl;
28 i = i + M;
29 }
30 else if (j == 0)
31 i = i + 1;
32 else
33 i = i + j; // slide the pattern by j
34 }
35}
36
37/* Driver code*/
38int main()
39{
40 string txt = "ABCEABCDABCEABCD";
41 string pat = "ABCD";
42 search(pat, txt);
43 return 0;
44}
45
46// This code is contributed by rathbhupendra
الگوریتم ساده بهینه برای جستجوی الگو در C
1/* C program for A modified Naive Pattern Searching
2 algorithm that is optimized for the cases when all
3 characters of pattern are different */
4#include<stdio.h>
5#include<string.h>
6
7/* A modified Naive Pettern Searching algorithn that is optimized
8 for the cases when all characters of pattern are different */
9void search(char pat[], char txt[])
10{
11 int M = strlen(pat);
12 int N = strlen(txt);
13 int i = 0;
14
15 while (i <= N - M)
16 {
17 int j;
18
19 /* For current index i, check for pattern match */
20 for (j = 0; j < M; j++)
21 if (txt[i+j] != pat[j])
22 break;
23
24 if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
25 {
26 printf("Pattern found at index %d \n", i);
27 i = i + M;
28 }
29 else if (j == 0)
30 i = i + 1;
31 else
32 i = i + j; // slide the pattern by j
33 }
34}
35
36/* Driver program to test above function */
37int main()
38{
39 char txt[] = "ABCEABCDABCEABCD";
40 char pat[] = "ABCD";
41 search(pat, txt);
42 return 0;
43}
الگوریتم ساده بهینه برای جستجوی الگو در جاوا
1/* Java program for A modified Naive Pattern Searching
2algorithm that is optimized for the cases when all
3characters of pattern are different */
4
5class GFG
6{
7
8/* A modified Naive Pettern Searching
9algorithn that is optimized for the
10cases when all characters of pattern are different */
11static void search(String pat, String txt)
12{
13 int M = pat.length();
14 int N = txt.length();
15 int i = 0;
16
17 while (i <= N - M)
18 {
19 int j;
20
21 /* For current index i, check for pattern match */
22 for (j = 0; j < M; j++)
23 if (txt.charAt(i + j) != pat.charAt(j))
24 break;
25
26 if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
27 {
28 System.out.println("Pattern found at index "+i);
29 i = i + M;
30 }
31 else if (j == 0)
32 i = i + 1;
33 else
34 i = i + j; // slide the pattern by j
35 }
36}
37
38/* Driver code*/
39public static void main (String[] args)
40{
41 String txt = "ABCEABCDABCEABCD";
42 String pat = "ABCD";
43 search(pat, txt);
44}
45}
46
47// This code is contributed by chandan_jnu
الگوریتم ساده بهینه برای جستجوی الگو در پایتون
1# Python program for A modified Naive Pattern Searching
2# algorithm that is optimized for the cases when all
3# characters of pattern are different
4def search(pat, txt):
5 M = len(pat)
6 N = len(txt)
7 i = 0
8
9 while i <= N-M:
10 # For current index i, check for pattern match
11 for j in xrange(M):
12 if txt[i+j] != pat[j]:
13 break
14 j += 1
15
16 if j==M: # if pat[0...M-1] = txt[i,i+1,...i+M-1]
17 print "Pattern found at index " + str(i)
18 i = i + M
19 elif j==0:
20 i = i + 1
21 else:
22 i = i+ j # slide the pattern by j
23
24# Driver program to test the above function
25txt = "ABCEABCDABCEABCD"
26pat = "ABCD"
27search(pat, txt)
28
29# This code is contributed by Bhavya Jain
الگوریتم ساده بهینه برای جستجوی الگو در #C
1/* C# program for A modified Naive Pattern Searching
2algorithm that is optimized for the cases when all
3characters of pattern are different */
4
5using System;
6
7class GFG
8{
9
10/* A modified Naive Pettern Searching
11algorithn that is optimized for the
12cases when all characters of pattern are different */
13static void search(string pat, string txt)
14{
15 int M = pat.Length;
16 int N = txt.Length;
17 int i = 0;
18
19 while (i <= N - M)
20 {
21 int j;
22
23 /* For current index i, check for pattern match */
24 for (j = 0; j < M; j++)
25 if (txt[i + j] != pat[j])
26 break;
27
28 if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
29 {
30 Console.WriteLine("Pattern found at index "+i);
31 i = i + M;
32 }
33 else if (j == 0)
34 i = i + 1;
35 else
36 i = i + j; // slide the pattern by j
37 }
38}
39
40/* Driver code*/
41static void Main()
42{
43 string txt = "ABCEABCDABCEABCD";
44 string pat = "ABCD";
45 search(pat, txt);
46}
47}
48
49// This code is contributed by chandan_jnu
الگوریتم ساده بهینه برای جستجوی الگو در PHP
1<?php
2// PHP program for A modified Naive
3// Pattern Searching algorithm that
4// is optimized for the cases when all
5// characters of pattern are different
6
7/* A modified Naive Pettern Searching
8 algorithn that is optimized for the
9 cases when all characters of
10 pattern are different */
11function search($pat, $txt)
12{
13 $M = strlen($pat);
14 $N = strlen($txt);
15 $i = 0;
16
17 while ($i <= $N - $M)
18 {
19 $j;
20
21 /* For current index i,
22 check for pattern match */
23 for ($j = 0; $j < $M; $j++)
24 if ($txt[$i + $j] != $pat[$j])
25 break;
26
27 // if pat[0...M-1] =
28 // txt[i, i+1, ...i+M-1]
29 if ($j == $M)
30 {
31 echo("Pattern found at index $i"."\n" );
32 $i = $i + $M;
33 }
34 else if ($j == 0)
35 $i = $i + 1;
36 else
37
38 // slide the pattern by j
39 $i = $i + $j;
40 }
41}
42
43// Driver Code
44$txt = "ABCEABCDABCEABCD";
45$pat = "ABCD";
46search($pat, $txt);
47
48// This code is contributed by nitin mittal.
49?>
خروجی قطعه کدهای بالا، به صورت زیر است.
Pattern found at index 4 Pattern found at index 12
اگر نوشته بالا برای شما مفید بوده است، آموزشهای زیر نیز به شما پیشنهاد میشوند:
- مجموعه آموزشهای برنامه نویسی
- آموزش ساختمان دادهها
- مجموعه آموزشهای ساختمان داده و طراحی الگوریتم
- رنگآمیزی گراف به روش حریصانه — به زبان ساده
- الگوریتم دایجسترا (Dijkstra) — از صفر تا صد
- الگوریتم پریم — به زبان ساده
- متن کاوی (Text Mining) — به زبان ساده
^^