KMP

Правка en2, от Heisenberg302, 2016-08-15 19:51:23

Hey, I have a question regarding Kmp. lets say I have a string s1 and a string s2. I want to know if s2 is in s1 or not. for this goal, can I add the second string s2 to the beginning of s1 and run preKmp algorithm , the code would look something like this :

        int n=toBeFound.size()+s.size(); //s is string in which toBeFound will be searched
	int i = 0 ; 
	int j= -1;
	s=toBeFound+s;
	int best =0;
	F[0]=-1;
	while (i < n) {
		while ( j>=0 && s[i] != s[j]) j = F[j];
		i++;
		j++;
		F[i] = j;
		if(j==toBeFound.size()){
			cout<<i-j-j<<endl; //starting index
			cout<<s.substr(i-j,j); //the found word
			break;
		}
	}

Edit: there should be some more conditions while checking the found string.

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en2 Английский Heisenberg302 2016-08-15 19:51:23 75
en1 Английский Heisenberg302 2016-08-15 18:44:08 669 Initial revision (published)