Skip to content

Instantly share code, notes, and snippets.

@manuchandel
Created August 25, 2015 16:42
Show Gist options
  • Save manuchandel/0fd71b79c38e07250cd8 to your computer and use it in GitHub Desktop.
Save manuchandel/0fd71b79c38e07250cd8 to your computer and use it in GitHub Desktop.
void longestPrefixSuffix(string &p,int *A,int n){
A[0]=0;
int i=1,j=0;
while(i<n){
if(p[i]==p[j]){ // match found
A[i]=j+1;
i++;
j++;
}else if(j==0){
A[i]=0;
i++;
}else j=A[j-1]; // search in the next longest prefix
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment