hdu 2222 Keywords Search ( AC 自动机)

Keywords Search

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 70366    Accepted Submission(s): 23954


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
 


记录模板

用了两种方法一种是malloc, 一种直接开完所有的节点, 不知道为什么第二种方法反而比较费时间。

第一种:

#include<iostream>
#include<cstdio> 
#include<queue>
#include<cstring>

using namespace std;
const int mx = 1e6 + 5;
char str[mx];

struct node{
  int fail, cnt;
  int next[26];
  
}tree[500100];       //少按了一个0 

int tot;

void add(node &a){
    a.cnt =0; 
	a.fail = -1;            //-1才是null , 0是root 
    
    memset(a.next, -1, sizeof(a.next));
}

void build1(char* te){
    int p ,q;
    int root = 0;
    int i, v, len =strlen(te);
    for(i =0, p= root; i<len; i++){
        v = te[i] - 'a';
        if( tree[p].next[v] == -1){
            q = ++tot;
            add(tree[q]);
            
            tree[p].next[v] = q;
        }
        p = tree[p].next[v];
    }
   tree[p].cnt++;
}
queue<int> q;

void build_ac( ){
    int root = 0;
    while(!q.empty()) q.pop();
    q.push(root);
    
    while(!q.empty()){
        int p = 0;
        int te = q.front();
        q.pop();
        for(int i = 0; i<26; i++){
            if(tree[te].next[i] == -1)
                continue;
            if(te == root)
               
               tree[tree[te].next[i]].fail = root;
            else{
            	 
                
                p = tree[te].fail;
                while(p != -1){
                
                    if(tree[p].next[i] != -1){
                        tree[tree[te].next[i]].fail =tree[p].next[i];
                        break; 
                    }
                    p = tree[p].fail;
                }
                if(p == -1)
                     tree[tree[te].next[i]].fail = root;
            }
            q.push(tree[te].next[i]);
        }
    }
}
int query(){
    int i, v, count = 0,root =0;
    int p = root;
    int len = strlen(str);
    for(int i = 0;  i< len; i++){
        v = str[i] - 'a';
        while(tree[p].next[v] == -1 && p != root)   
          p = tree[p].fail;
          
        p = tree[p].next[v];    
        if(p == -1)
          p = root;       
        int te = p;
        while(te != root){
            if(tree[te].cnt >= 0){
                count += tree[te].cnt;
                tree[te].cnt = -1;
            }
            else 
               break;
            te = tree[te].fail;
        }
    }
    
    return count;
}
int main(){
    int T, n;
   //freopen("123.txt", "r", stdin);
    char te[55];
    
    scanf("%d", &T);
   
    while(T--){
        add(tree[0]);       //节点初始化 
        
        scanf("%d", &n);
        
        tot  = 0;
        while(n--){
        
            scanf("%s", te);
            build1(te);
        }
        
        scanf("%s", str);
        
        build_ac();
        
        printf("%d\n", query());
       
    }
    
    
    return 0;
}

第二种

#include<iostream>
#include<cstdio> 
#include<queue>
#include<cstring>
#include<cstdlib>
using namespace std;
const int mx = 1e6 + 5;
char str[mx];

struct node{
  node* fail;
  node* next[26];
  int cnt;
  
}*root;

void add(node* root){
    root->cnt = 0;
    root->fail = NULL;
    for(int i = 0; i < 26; i++){
        root->next[i] = NULL; 
    }
}

void build1(char* te){
    node *p ,*q;
    int i, v, len =strlen(te);
    for(i =0, p= root; i<len; i++){
        v = te[i] - 'a';
        if( p->next[v] == NULL){
            q = (struct node*)malloc(sizeof(node));
            add(q);
            
            p->next[v] = q;
        }
        p = p->next[v];
    }
    p->cnt++;
}
queue<node*> q;

void build_ac(node* root){
    while(!q.empty()) q.pop();
    q.push(root);
    
    while(!q.empty()){
        node* p = NULL;
        node* te = q.front();
        q.pop();
        for(int i = 0; i<26; i++){
            if(te->next[i] == NULL)
                continue;
            if(te == root)
               te->next[i]->fail = root;
            else{
                p = te->fail;
                while(p != NULL){
                	//	cout<<"jin"<<endl;
                    if(p->next[i] != NULL){
                        te->next[i]->fail = p->next[i];
                        break; 
                    }
                    p = p->fail;
                }
                if(p == NULL)
                      te->next[i]->fail = root;
            }
            q.push(te->next[i]);
        }
    }
}
int query(node* root){
    int i, v, count = 0;
    node* p = root;
    int len = strlen(str);
    for(int i = 0;  i< len; i++){
        v = str[i] - 'a';
        while( p->next[v] == NULL && p != root)  //next[v] 错写成 next  
          p = p->fail;
          
        p = p->next[v];    // v错写成 i 
        if(p == NULL )
          p = root;       //这里写错 
        node* te = p;
        while(te != root){
            if(te->cnt >= 0){
                count += te->cnt;
                te->cnt = -1;
            }
            else 
               break;
            te = te->fail;
        }
    }
    
    return count;
}
int main(){
    int T, n;
    char te[55];
    //freopen("123.txt", "r", stdin);
    scanf("%d", &T);
    while(T--){
        root=(struct node*)malloc(sizeof(node));
        add(root);
        
        scanf("%d", &n);
        while(n--){
            scanf("%s", te);
            build1(te);
        }
        scanf("%s", str);
        build_ac(root);
        //cout<<query(root)<<endl;
        printf("%d\n", query(root));
    }
    
    
    return 0;
} 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blaze Jack

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值