Advertisement
nasarouf

aho-corasic

Apr 20th, 2017
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.90 KB | None | 0 0
  1. //aho-corasic (find needles in a haystack)
  2. // nasarouf@cs.ubc.ca
  3.  
  4. //old untested code... but should work :)
  5.  
  6. /***********************************************************************/
  7. /**      AhoCorasic.h                                                 **/
  8. /**                                                                   **/
  9. /**      (c) 2006                            **/
  10. /**                                                                   **/
  11. /**      Update history:                                              **/
  12. /**        Created:18/01/2006  Mushfiqur Rouf                         **/
  13. /**                                                                   **/
  14. /***********************************************************************/
  15. #ifndef _AHOCORASIC_H_
  16. #define _AHOCORASIC_H_
  17.  
  18. #include <map>
  19. using namespace std;
  20.  
  21. template<class ELEM_TYPE,class INDEX_TYPE>
  22. class AhoNode{
  23.     typedef map<ELEM_TYPE,AhoNode*> ahomap;
  24.     ahomap c;
  25.     AhoNode *next,*root;
  26.     INDEX_TYPE index; //positive index
  27.          
  28. public:
  29.     AhoNode(AhoNode*);
  30.     AhoNode();
  31.     ~AhoNode();
  32.     void clear();
  33.     void build();
  34.     void addPattern(ELEM_TYPE* pat, INDEX_TYPE index);
  35.     int search(ELEM_TYPE *s, vector<INDEX_TYPE> &matchlist);
  36.  
  37. private:
  38.     void build0();
  39.  
  40. };
  41.  
  42. #endif //_AHOCORASIC_H_
  43.  
  44.  
  45. /***********************************************************************/
  46. /**      AhoCorasic.cpp                                               **/
  47. /**                                                                   **/
  48. /**      (c) 2006                            **/
  49. /**                                                                   **/
  50. /**      Update history:                                              **/
  51. /**        Created:18/01/2006  Mushfiqur Rouf                         **/
  52. /**                                                                   **/
  53. /***********************************************************************/
  54. #include "StdAfx.h"
  55. #include "AhoCorasic.h"
  56. #include <map>
  57. using namespace std;
  58.  
  59. template<class ELEM_TYPE,class INDEX_TYPE>
  60. AhoNode<ELEM_TYPE,INDEX_TYPE>::AhoNode(AhoNode<ELEM_TYPE,INDEX_TYPE>* ahoNode=NULL){
  61.     if (ahoNode==NULL)
  62.             root=this;
  63.     else
  64.             root=ahoNode;
  65.     next=root;
  66.     index=0;
  67. }
  68.  
  69. template<class ELEM_TYPE,class INDEX_TYPE>
  70. AhoNode<ELEM_TYPE,INDEX_TYPE>::~AhoNode(){
  71.     clear();
  72. }
  73.  
  74. template<class ELEM_TYPE,class INDEX_TYPE>
  75. void AhoNode<ELEM_TYPE,INDEX_TYPE>::clear(){
  76.     ahomap::iterator i;
  77.     for (i=c.begin(); i!=c.end(); i++)
  78.             delete((AhoNode<ELEM_TYPE,INDEX_TYPE>*)((*i).second));
  79.     c.clear();
  80.     next=root;
  81. }
  82.  
  83. template<class ELEM_TYPE,class INDEX_TYPE>
  84. void AhoNode<ELEM_TYPE,INDEX_TYPE>::addPattern(ELEM_TYPE *pat, INDEX_TYPE index){
  85.     int i;
  86.     AhoNode *ahoNode=root;
  87.     for (i=0; pat[i]; i++){
  88.             if (ahoNode->c.find(pat[i])==ahoNode->c.end())
  89.                     ahoNode->c[pat[i]]=new AhoNode(root);
  90.             ahoNode=ahoNode->c[pat[i]];
  91.     }
  92.     ahoNode->index=index;
  93. }
  94.  
  95. template<class ELEM_TYPE,class INDEX_TYPE>
  96. void AhoNode<ELEM_TYPE,INDEX_TYPE>::build0(){
  97.     ahomap::iterator i;
  98.     ELEM_TYPE key;
  99.     AhoNode<ELEM_TYPE,INDEX_TYPE> *p,*child;
  100.     for (i=c.begin(); i!=c.end(); i++){
  101.             key=(*i).first;
  102.             child=p=(*i).second;
  103.             while(p && p->c.find(key)!=p->c.end())
  104.                     p=p->next;
  105.             if (p==NULL)
  106.                     child->next=root;
  107.             else
  108.                     child->next=p->c[key];
  109.             child->build();
  110.     }
  111. }
  112.  
  113. template<class ELEM_TYPE,class INDEX_TYPE>
  114. void AhoNode<ELEM_TYPE,INDEX_TYPE>::build(){
  115.     ahomap::iterator i;
  116.     ELEM_TYPE key;
  117.     AhoNode<ELEM_TYPE,INDEX_TYPE> *p,*child;
  118.     for (i=c.begin(); i!=c.end(); i++){
  119.             key=(*i).first;
  120.             child=p=(*i).second;
  121.             child->next=root;
  122.             child->build();
  123.     }
  124. }
  125.  
  126. template<class ELEM_TYPE,class INDEX_TYPE>
  127. int AhoNode<ELEM_TYPE,INDEX_TYPE>::search(ELEM_TYPE *s, vector<INDEX_TYPE> &matchlist){
  128.     int i,res=0;
  129.     ELEM_TYPE k;
  130.     AhoNode<ELEM_TYPE,INDEX_TYPE> *p=root,*q;
  131.     for (; *s; s++){
  132.             k=*s;
  133.             while(p && p->c.find(k)==p->c.end())
  134.                     p=p->next;
  135.             if (p==NULL){
  136.                     p=root;
  137.                     continue;
  138.             }
  139.             p=p->c[k];
  140.             q=p;
  141.             do{
  142.                     if (q->index>0){
  143.                             res++;
  144.                             matchlist.push_back(q->index);
  145.                     }
  146.                     q=q->next;
  147.             }while(q!=NULL);
  148.     }
  149.     return res;
  150. }
  151.  
  152. /*void print(AhoNode *AhoNode,int lev){
  153.     int i,j;
  154.     for (i=0; i<52; i++){
  155.             if (AhoNode->c[i]==NULL) continue;
  156.             for (j=0; j<lev; j++)
  157.                     printf("   ");
  158.             if (i<26) printf("%c ",'a'+i);
  159.             else printf("%c ",'A'+i-26);
  160.             printf(" - id=%d",(AhoNode->c[i]-AhoNodes));
  161.             if (AhoNode->c[i]->next!=NULL) printf(" next=%d",(AhoNode->c[i]->next-AhoNodes));
  162.             if (AhoNode->c[i]->index) printf(" *%d* ",AhoNode->c[i]->index);
  163.             printf("\n");
  164.             print(AhoNode->c[i],lev+1);
  165.     }
  166. }*/
  167.  
  168. /*int main(){
  169.     int ts,u,i,j,k;
  170.     ELEM_TYPE pat[1001];
  171.     AhoNode *p,*q;
  172. //    freopen("input.txt","rt",stdin);
  173. //    freopen("output.txt","wt",stdout);
  174.     init();
  175.     scanf("%d",&ts);
  176.     for (u=0; u<ts; u++){
  177.             scanf("%s%d",s,&n);
  178.             nc=0;
  179.             root=newAhoNode();
  180.             root->next=NULL;
  181.             for (i=0; i<n; i++)
  182.                     next[i]=0;
  183.             for (i=0; i<n; i++){
  184.                     scanf("%s",pat);
  185.                     add(pat,i);
  186.                     found[i]=0;
  187.             }
  188.             build0();
  189.             p=root;
  190.             for (i=0; s[i]; i++){
  191.                     k=map[s[i]];
  192.                     while(p && p->c[k]==NULL)
  193.                             p=p->next;
  194.                     if (p==NULL){
  195.                             p=root;
  196.                             continue;
  197.                     }
  198.                     p=p->c[k];
  199.                     q=p;
  200.                     do{
  201.                             if (q->index>0) found[q->index-1]=1;
  202.                             q=q->next;
  203.                     }while(q!=NULL);
  204.                     //else if (p->index>0) found[p->index-1]=1;
  205.             }
  206.             for (i=0; i<n; i++){
  207.                     if (found[i]){
  208.                             j=i;
  209.                             while(next[j]>0){
  210.                                     j=next[j]-1;
  211.                                     found[j]=1;
  212.                             }
  213.                     }
  214.             }
  215.             for (i=0; i<n; i++){
  216.                     if (found[i]) printf("yes\n");
  217.                     else printf("no\n");
  218.             }
  219.             //print(root,0);
  220.     }
  221.     return 0;
  222. }
  223. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement