File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 1+ #include  < bits/stdc++.h>   
2+ using  namespace  std ;  
3+ # define  NO_OF_CHARS  256   
4+   
5+ void  badCharHeuristic ( string str, int  size,  
6+                         int  badchar[NO_OF_CHARS])  
7+ {  
8+     int  i;  
9+   
10+     for  (i = 0 ; i < NO_OF_CHARS; i++)  
11+         badchar[i] = -1 ;  
12+   
13+     for  (i = 0 ; i < size; i++)  
14+         badchar[(int ) str[i]] = i;  
15+ }  
16+  
17+ void  search ( string txt, string pat)  
18+ {  
19+     int  m = pat.size ();  
20+     int  n = txt.size ();  
21+   
22+     int  badchar[NO_OF_CHARS];  
23+   
24+     badCharHeuristic (pat, m, badchar);  
25+   
26+     int  s = 0 ; 
27+     while (s <= (n - m))  
28+     {  
29+         int  j = m - 1 ;  
30+   
31+         while (j >= 0  && pat[j] == txt[s + j])  
32+             j--;  
33+   
34+         if  (j < 0 )  
35+         {  
36+             cout << " pattern occurs at shift = "   <<  s << endl;  
37+   
38+             s += (s + m < n)? m-badchar[txt[s + m]] : 1 ;  
39+   
40+         }  
41+   
42+         else 
43+             s += max (1 , j - badchar[txt[s + j]]);  
44+     }  
45+ }  
46+   
47+ /*  main func*/ 
48+ int  main ()  
49+ {  
50+     string txt= " ABAAABCD"  ;  
51+     string pat = " ABC"  ;  
52+     search (txt, pat);  
53+     return  0 ;  
54+ }  
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments