Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2417184
  • 博文数量: 392
  • 博客积分: 7040
  • 博客等级: 少将
  • 技术积分: 4138
  • 用 户 组: 普通用户
  • 注册时间: 2009-06-17 13:03
个人简介

范德萨发而为

文章分类

全部博文(392)

文章存档

2017年(5)

2016年(19)

2015年(34)

2014年(14)

2013年(47)

2012年(40)

2011年(51)

2010年(137)

2009年(45)

分类: C/C++

2011-02-28 12:08:13

 

有了这些数据结构之后,就可以开始编程了:
   
首先,将这5个单词构造成一棵Tire,如图-1所示。

 

 1 void insert(char *str,node *root){ 
 2     node *p=root; 
 3     int i=0,index;  
 4     while(str[i]){ 
 5         index=str[i]-'a'
 6         if(p->next[index]==NULL) p->next[index]=new node();  
 7         p=p->next[index];
 8         i++;
 9     } 
10     p->count++;     //在单词的最后一个节点count+1,代表一个单词
11 }

在构造完这棵Tire之后,接下去的工作就是构造下失败指针。构造失败指针的过程概括起来就一句话:设这个节点上的字母为C,沿着他父亲的失败指针走,直到走到一个节点,他的儿子中也有字母为C的节点。然后把当前节点的失败指针指向那个字母也为C的儿子。如果一直走到了root都没找到,那就把失败指针指向root。具体操作起来只需要:先把root加入队列(root的失败指针指向自己或者NULL),这以后我们每处理一个点,就把它的所有儿子加入队列,队列为空。

 1 void build_ac_automation(node *root){
 2     int i;
 3     root->fail=NULL; 
 4     q[head++]=root; 
 5     while(head!=tail){ 
 6         node *temp=q[tail++]; 
 7         node *p=NULL; 
 8         for(i=0;i<26;i++){ 
 9             if(temp->next[i]!=NULL){ 
10                 if(temp==root) temp->next[i]->fail=root;                 
11                 else
12                     p=temp->fail; 
13                     while(p!=NULL){  
14                         if(p->next[i]!=NULL){ 
15                             temp->next[i]->fail=p->next[i]; 
16                             break
17                         } 
18                         p=p->fail; 
19                     } 
20                     if(p==NULL) temp->next[i]->fail=root; 
21                 } 
22                 q[head++]=temp->next[i];  
23             } 
24         }   
25     } 
26 }

    从代码观察下构造失败指针的流程:对照图-2来看,首先rootfail指针指向NULL,然后root入队,进入循环。第1次循环的时候,我们需要处理2个节点:root->next[‘h’-‘a’](节点h) root->next[‘s’-‘a’](节点s)。把这2个节点的失败指针指向root,并且先后进入队列,失败指针的指向对应图-2中的(1)(2)两条虚线;第2次进入循环后,从队列中先弹出h,接下来p指向h节点的fail指针指向的节点,也就是root;进入第13行的循环后,p=p->fail也就是p=NULL,这时退出循环,并把节点efail指针指向root,对应图-2中的(3),然后节点e进入队列;第3次循环时,弹出的第一个节点a的操作与上一步操作的节点e相同,把afail指针指向root,对应图-2中的(4),并入队;第4次进入循环时,弹出节点h(图中左边那个),这时操作略有不同。在程序运行到14行时,由于p->next[i]!=NULL(rooth这个儿子节点,图中右边那个),这样便把左边那个h节点的失败指针指向右边那个root的儿子节点h,对应图-2中的(5),然后h入队。以此类推:在循环结束后,所有的失败指针就是图-2中的这种形式。

  最后,我们便可以在AC自动机上查找模式串中出现过哪些单词了。匹配过程分两种情况:(1)当前字符匹配,表示从当前节点沿着树边有一条路径可以到达目标字符,此时只需沿该路径走向下一个节点继续匹配即可,目标字符串指针移向下个字符继续匹配;(2)当前字符不匹配,则去当前节点失败指针所指向的字符继续匹配,匹配过程随着指针指向root结束。重复这2个过程中的任意一个,直到模式串走到结尾为止。
 1 int query(node *root){ 
 2     int i=0,cnt=0,index,len=strlen(str); 
 3     node *p=root;  
 4     while(str[i]){  
 5         index=str[i]-'a';  
 6         while(p->next[index]==NULL && p!=root) p=p->fail; 
 7         p=p->next[index]; 
 8         p=(p==NULL)?root:p; 
 9         node *temp=p; 
10         while(temp!=root && temp->count!=-1){ 
11             cnt+=temp->count; 
12             temp->count=-1
13             temp=temp->fail; 
14         } 
15         i++;                 
16     }    
17     return cnt; 
18 }
    对照图-2,看一下模式匹配这个详细的流程,其中模式串为yasherhs。对于i=0,1。Trie中没有对应的路径,故不做任何操作;i=2,3,4 时,指针p走到左下节点e。因为节点e的count信息为1,所以cnt+1,并且讲节点e的count值设置为-1,表示改单词已经出现过了,防止重复计数,最后temp指向e节点的失败指针所指向的节点继续查找,以此类推,最后temp指向root,退出while循环,这个过程中count增加了 2。表示找到了2个单词she和he。当i=5时,程序进入第5行,p指向其失败指针的节点,也就是右边那个e节点,随后在第6行指向r节点,r节点的 count值为1,从而count+1,循环直到temp指向root为止。最后i=6,7时,找不到任何匹配,匹配过程结束。

    到此为止AC自动机算法的详细过程已经全部介绍结束,看一道例题:
Problem Description
In the modern time, Search engine came into the life of everybody like Google, Baidu, etc.
Wiskey also wants to bring this feature to his image retrieval system.
Every image have a long description, when users type some keywords to find the image, the system will match the keywords with description of image and show the image which the most keywords be matched.
To simplify the problem, giving you a description of image, and some keywords, you should tell me how many keywords will be match.
 

Input
First line will contain one integer means how many cases will follow by.
Each case will contain two integers N means the number of keywords and N keywords follow. (N <= 10000)
Each keyword will only contains characters 'a'-'z', and the length will be not longer than 50.
The last line is the description, and the length will be not longer than 1000000.
 

Output
Print how many keywords are contained in the description.
 

Sample Input
1 5 she he say shr her yasherhs
 

Sample Output
3

 1 #include <iostream> 
 2 using namespace std; 
 3   
 4 const int kind = 26
 5 struct node{  
 6     node *fail;       //失败指针
 7     node *next[kind]; //Tire每个节点的26个子节点(最多26个字母)
 8     int count;        //是否为该单词的最后一个节点
 9     node(){           //构造函数初始化
10         fail=NULL; 
11         count=0
12         memset(next,NULL,sizeof(next)); 
13     } 
14 }*q[500001];          //队列,方便用于bfs构造失败指针
15 char keyword[51];     //输入的单词
16 char str[1000001];    //模式串
17 int head,tail;        //队列的头尾指针
18   
19 void insert(char *str,node *root){ 
20     node *p=root; 
21     int i=0,index;  
22     while(str[i]){ 
23         index=str[i]-'a'
24         if(p->next[index]==NULL) p->next[index]=new node();  
25         p=p->next[index];
26         i++;
27     } 
28     p->count++
29 
30 void build_ac_automation(node *root){
31     int i;
32     root->fail=NULL; 
33     q[head++]=root; 
34     while(head!=tail){ 
35         node *temp=q[tail++]; 
36         node *p=NULL; 
37         for(i=0;i<26;i++){ 
38             if(temp->next[i]!=NULL){ 
39                 if(temp==root) temp->next[i]->fail=root;                 
40                 else
41                     p=temp->fail; 
42                     while(p!=NULL){  
43                         if(p->next[i]!=NULL){ 
44                             temp->next[i]->fail=p->next[i]; 
45                             break
46                         } 
47                         p=p->fail; 
48                     } 
49                     if(p==NULL) temp->next[i]->fail=root; 
50                 } 
51                 q[head++]=temp->next[i];  
52             } 
53         }   
54     } 
55 
56 int query(node *root){ 
57     int i=0,cnt=0,index,len=strlen(str); 
58     node *p=root;  
59     while(str[i]){  
60         index=str[i]-'a';  
61         while(p->next[index]==NULL && p!=root) p=p->fail; 
62         p=p->next[index]; 
63         p=(p==NULL)?root:p; 
64         node *temp=p; 
65         while(temp!=root && temp->count!=-1){ 
66             cnt+=temp->count; 
67             temp->count=-1
68             temp=temp->fail; 
69         } 
70         i++;                 
71     }    
72     return cnt; 
73 
74 int main(){ 
75     int n,t; 
76     scanf("%d",&t); 
77     while(t--){  
78         head=tail=0
79         node *root=new node(); 
80         scanf("%d",&n); 
81         getchar(); 
82         while(n--){ 
83             gets(keyword); 
84             insert(keyword,root); 
85         } 
86         build_ac_automation(root); 
87         scanf("%s",str); 
88         printf("%d\n",query(root));  
89     } 
90     return 0
91 }

    PS:原创,转载请注明出处
http://www.cppblog.com/mythit/archive/2009/04/21/80633.html
 
 
http://hi.baidu.com/lightxianjian/blog/item/d0f0b8de8041125095ee3710.html
阅读(1247) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~