Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main()
- {
- int n,m;
- scanf("%d %d",&n,&m);
- char str1[n+1];
- char str2[m+1];
- scanf(" %s %s",str1,str2);
- int LPS[m+1];
- LPS[0] = 0;
- for(int i = 1 , j = 0 ; i < m ; ){
- if(str2[i] == str2[j]){
- LPS[i] = j + 1;
- i ++ ; j ++;
- }
- else if(j > 0){
- j = LPS[j-1];
- }
- else{
- LPS[i] = 0;
- i ++;
- }
- }
- // printf("Test LPS : ");for(int i = 0 ; i < m ; i ++ )printf("%d ",LPS[i]);printf("\n");
- int cnt = 0;
- for(int i = 0 , j = 0 ; i < n ; ){
- if(str1[i] == str2[j]){
- i ++ ; j ++;
- if(j == m){ //match
- cnt++;
- j = LPS[j-1];
- }
- }
- else if(j > 0){
- j = LPS[j-1];
- }
- else{
- i ++;
- }
- }
- printf("%d",cnt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement