#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
void Pinggiran(char P[], int F[],int m){
int i,j;
F[0]=0;
j=0;
i=1;
while(i<m){
if(P[i]==P[j]){
F[i]=j+1;
i++;
j++;
}else if(j>0){
j=F[j-1];
}else {
F[i]=0;
i++;
}
}
}
int KMPsearch(char T[], char P[]){
int i,j,F[100];
int m=strlen(P);
int n=strlen(T);
Pinggiran(P,F,m);
i=0;
j=0;
while(i<n){
if(T[i]==P[j]){
if(j==m-1)
return i-j;
else{
i++;
j++;
}
}else if(j>0){
j=F[j-1];
}else{
i++;
}
}
return -1;
}
int main()
{
char T[1000],P[100];
while(gets(T)){
gets(P);
int pos=KMPsearch(T,P);
if(pos!=-1)
cout<<"String Ditemukan pada Posisi: "<<pos<<endl;
else
cout<<"Tidak ada hasil"<<endl;
}
return 0;
}
Praktikum #10 Strategi Algoritma
Monday, June 2, 2014
- Posted by Unknown
- at 9:11 PM
- Email ThisBlogThis!Share to XShare to Facebook
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment