zoukankan      html  css  js  c++  java
  • hdu2222Keywords Search

    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
     
    Author
    Wiskey
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    
    using namespace std;
    
    struct node
    {
        node* next[26];
        int end;
        node* fail;
        node()
        {
            for(int i = 0;i<26;i++)
                next[i] = NULL;
            fail = NULL;
            end = 0;
        }
    };
    node* root;
    char s[51],ss[1100000];
    void insert()
    {
        int i,l = strlen(s);
        node* k = root;
        for(i  = 0;i<l;i++)
        {
            int id = s[i]-'a';
            if(k->next[id] == NULL)
                k->next[id] = new node();
            k = k->next[id];
        }
        k->end++;
    }
    void build()
    {
        queue<node*> q;
        for(int i = 0;i<26;i++)
        {
            node* k = root;
            if(k->next[i] != NULL)
            {
                k->next[i]->fail = root;
                q.push(k->next[i]);
            }
        }
        while(!q.empty())
        {
            node*k = q.front();
            q.pop();
            for(int i = 0;i<26;i++)
            {
                if(k->next[i]!=NULL)
                {
                    node*t = k->fail;
                    while(t!=root&&t->next[i] == NULL) t = t->fail;
                    if(t->next[i] != NULL) t = t->next[i];
                    k->next[i]->fail = t;
                    q.push(k->next[i]);
                }
            }
        }
    }
    int ask()
    {
        int i,l = strlen(ss),ans = 0;
        node *k = root;
        for(i = 0;i<l;i++)
        {
            int id = ss[i]-'a';
            while(k!=root&&k->next[id] == NULL) k = k->fail;
            if(k->next[id]!=NULL) k = k->next[id];
            node* t = k;
            while(t!=root)
            {
                ans += t->end;
                t->end = 0;
                t = t->fail;
            }
        }
        return ans;
    }
    
    
    int main()
    {
        int z;
        int n,i,j,k;
        cin>>z;
        while(z--)
        {
            root = new node();
            scanf("%d",&n);
            while(n--)
            {
                scanf("%s",s);
                insert();
            }
            build();
            scanf("%s",ss);
            printf("%d
    ",ask());
        }
        return 0;
    }
  • 相关阅读:
    DXF文件格式理解
    Qt Creator调试命令行参数
    qt编译错误:invalid new-expression of abstract class type "*"
    dxf文件解析库libdxf初探
    ubuntu linux编译libdxfrw
    CAD DWG/DXF文件C++解析库libdxfrw
    AutoCAD版本dxf文件与dxflib解析库支持探索
    Linux 6种日志查看方法(转)
    码率、分辨率、帧率和画质计算单位时间码流大小(转)
    纯手工打造车载车牌识别检测系统(转)
  • 原文地址:https://www.cnblogs.com/wos1239/p/4398942.html
Copyright © 2011-2022 走看看