nasarouf

aho-corasic

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