Advertisement
NonWhite

Aho-corasick

Aug 1st, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <set>
  6. #include <map>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <stack>
  11. #include <sstream>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <cctype>
  15. #define clr( x , y ) memset( x ,y , sizeof x )
  16. #define FOR(i,A) for(typeof (A).begin() i = (A).begin() ; i != (A).end() ; i++)
  17. #define mp make_pair
  18. #define TAM 100010
  19. #define MOD 1000000007
  20. #define debug( x ) cout << #x << " = " << x << endl
  21. #define f(i,n) for(int i = 0 ; i < n ; i++)
  22. #define ff(i,a,b) for(int i = a ; i < b ; i++)
  23. #define all( x ) x.begin() , x.end()
  24. #define ral( x ) x.rbegin() , x.rend()
  25.  
  26. using namespace std ;
  27.  
  28. typedef pair<int,int> ii ;
  29. typedef pair<ii,ii> pii ;
  30. typedef long long ll ;
  31. typedef long double ld ;
  32.  
  33. struct arist{
  34.     bool final ;
  35.     int next[ 300 ] ;
  36.     int failure ;
  37.  
  38.     void clean(){
  39.         clr( next , -1 ) ;
  40.         failure = -1 ;
  41.         final = false ;
  42.     }
  43. } ;
  44.  
  45. arist trie[ TAM ] ;
  46. int pal ;
  47. string alph ;
  48.  
  49. int nextState( int q , char w ){
  50.     if( q < 0 ) return 0 ;
  51.     if( trie[ q ].next[ w ] != -1 ) return trie[ q ].next[ w ] ;
  52.     if( trie[ q ].failure != -1 ) return nextState( trie[ q ].failure , w ) ;
  53.     return 0 ;
  54. }
  55.  
  56. void getFailure(){
  57.     queue<int> q ;
  58.     q.push( 0 ) ;
  59.     while( !q.empty() ){
  60.         int nodo = q.front() ; q.pop() ;
  61.         for(char c = 0 ; c < alph.size() ; c++){
  62.             if( trie[ nodo ].next[ c ] != -1 ){
  63.                 int p = trie[ nodo ].next[ c ] ;
  64.                 trie[ p ].failure = nextState( trie[ nodo ].failure , c ) ;
  65.                 if( trie[ trie[ p ].failure ].final )
  66.                     trie[ p ].final = true ;
  67.                 q.push( p ) ;
  68.             }
  69.         }
  70.     }
  71. }
  72.  
  73. char toDict( char c ){
  74.     alph += c ;
  75.     return c ;
  76. }
  77.  
  78. void build( string S ){
  79.     int n = S.size() ;
  80.     int nodo = 0 ;
  81.     for(int i = 0 ; i < n ; i++){
  82.         char c = toDict( S[ i ] ) ;
  83.         if( trie[ nodo ].next[ c ] == -1 ) trie[ nodo ].next[ c ] = pal++ ;
  84.         nodo = trie[ nodo ].next[ c ] ;
  85.     }
  86.     trie[ nodo ].final = true ;
  87.  
  88.     getFailure() ;
  89. }
  90.  
  91. void init(){
  92.     pal = 1 ;
  93.     for(int i = 0 ; i < TAM ; i++) trie[ i ].clean() ; 
  94.     alph = "" ;
  95. }
  96.  
  97. bool match( string S ){
  98.     int n = S.size() ;
  99.     int nodo = 0 ;
  100.     for(int i = 0 ; i < n ; i++){
  101.         char c = S[ i ] ;
  102.         if( trie[ nodo ].next[ c ] == -1 ) return false ;
  103.  
  104.         nodo = nextState( nodo , c ) ;
  105.     }
  106.     return true ;
  107. //  return trie[ nodo ].final ;
  108. }
  109.  
  110. int main(){
  111.  
  112.     int test , q ;
  113.     char S[ TAM ] ;
  114.     scanf("%d" , &test ) ;
  115.     for(int k = 0 ; k < test ; k++){
  116.         init() ;
  117.         scanf("%s" , S ) ;
  118.         build( S ) ;
  119.         scanf("%d" , &q ) ;
  120.         for(int i = 0 ; i < q ; i++){
  121.             scanf("%s" , S ) ;
  122.             int resp = match( S ) ;
  123.             printf("%c\n" , resp ? 'y' : 'n' ) ;
  124.         }
  125.     }
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement