procedure PencocokanString(input P : string, T: string, n, m : integer, output idx : integer)
{ Masukan: pattern P yang panjangnya m dan teks
T yang panjangnya n. Teks T direpresentasika
sebagai string (array of character)
Keluaran: lokasi awal kecocokan (idx)
}
Deklarasi
i : integer
ketemu : boolean
Algoritma:
i <-- 0
ketemu <-- false
while (i <-- n-m) and (not ketemu) do
j <-- 1
while (j <-- m) and (Pj = Ti+j ) do
j <-- j+1
endwhile
{ j > m or Pj <-- Ti+j }
if j = m then { kecocokan string
ditemukan }
ketemu <-- true
else
i <-- i+1 {geser pattern satu karakter ke
kanan teks }
endif
endfor
{ i > n – m or ketemu }
if ketemu then
idx <-- i+1
else
idx <-- -1
endif
Program
#include <cstdlib>
#include <iostream>
using namespace std;
void PencocokanString( string P, string T, int n, int m, int idx){
int i;
int j;
bool ketemu;
i = 0;
ketemu = false;
while (( i <= n - m ) && ( ! ketemu)){
j == 1;
while (( j <= m ) && ( P[j] = T[i + j])){
j = j + 1;
}
//{ j > m or P[j] =! T[i + j]}
if ( j == m ){
ketemu = true;
}else {
i = i + 1;
}
}
//{ i > n - m or ketemu }
if ( ketemu ){
idx = i + 1;
}else{
idx = -1;
}
}
int main(int argc, char *argv[])
{
int idx;
string T;
string P;
cout<<"Masukkan teks = ";
cin>>T;
cout<<"Masukksn kata yang dicari (pattern) = ";
cin>>P;
cout<<"Ditemukan di karakter ke = "<<idx;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Running
Tidak ada komentar:
Posting Komentar