zoukankan      html  css  js  c++  java
  • HDU

    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

    如果完全不會AC自動機的可以先看看這位大佬的博客:https://blog.csdn.net/creatorx/article/details/71100840

    代碼:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    typedef struct Node* node;
    
    const int MAXNs = 55 ;//模式串最大長度 
    const int MAXNS = 1000005;//文章(待匹配串)最大長度 
    
    struct Node{
        node next[26];
        node fail;//失配指针
        int sum;
        Node(){
        	sum = 0;
        	fail = NULL;
        	memset(next,NULL,sizeof next);
    	}
    };
    
    char s[MAXNs];
    
    void Insert(node root)//字典树的建立
    {
        node p = root;
        int len = strlen(s);
        for(int i=0 ; i<len ; ++i)
        {
            int x = s[i] - 'a';
            if(p->next[x] == NULL)
            {
                node newnode = new Node();
                p->next[x] = newnode;
            }
            p = p->next[x];
        }
        p->sum++;
    }
    
    void build_fail_pointer(node root)//构造fail指针
    {
        queue<node> Q;
        Q.push(root);
        node p,temp;
        while(!Q.empty())
        {
            temp = Q.front();
            Q.pop();
            for(int i=0 ; i<26 ; ++i)
            {
                if(temp->next[i])
                {
                    if(temp == root)
                    {
                        temp->next[i]->fail = root;
                    }
                    else
                    {
                        p = temp->fail;
                        while(p)
                        {
                            if(p->next[i])
                            {
                                temp->next[i]->fail = p->next[i];
                                break;
                            }
                            p = p->fail;
                        }
                        if(p == NULL) temp->next[i]->fail = root;
                    }
                    Q.push(temp->next[i]);
                }
            }
        }
    }
    
    char S[MAXNS];
    
    int ac_automation(node root)//利用fail指针进行匹配。
    {
        node p = root;
        int len = strlen(S);
        int ans = 0;
        for(int i=0 ; i<len ; ++i)
        {
            int x = S[i] - 'a';
            while(!p->next[x] && p != root) p = p->fail;
            p = p->next[x];
            if(!p) p = root;
            node temp = p;
            while(temp != root)
            {
               if(temp->sum >= 0)
               {
                   ans += temp->sum;
                   temp->sum = -1;
               }
               else break;
               temp = temp->fail;
            }
        }
        return ans;
    }
    
    void Del(node root){
    	for(int i=0 ; i<26 ; ++i){
    		if(root->next[i])Del(root->next[i]);
    	}
    	delete(root);
    }
    	
    int main(){
    	
    	int T;
    	scanf("%d",&T);
    	while(T--){
    		int N;
    		node root = new Node();
    		scanf("%d",&N);
    		while(N--){
    			scanf("%s",s);
    			Insert(root);
    		}
    		scanf("%s",S);
    		build_fail_pointer(root);
    		printf("%d
    ",ac_automation(root));
    		Del(root);
    	}
    	
    	return 0;
    }
    
  • 相关阅读:
    ubuntu系统安装微信小程序开发工具
    【工具】vscode-代码编辑器详解
    微信小程序开发
    webpack基本配置
    vue相关知识
    史上最强vue总结~万字长文---面试开发全靠它了
    ES6——字符串
    ES6——Proxy与Reflect
    ES6——Map与Set
    ES6——Symbol
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514017.html
Copyright © 2011-2022 走看看