zoukankan      html  css  js  c++  java
  • HDU

     
    Time Limit: 1000MS   Memory Limit: 131072KB   64bit IO Format: %I64d & %I64u

     Status

    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
     

    AC自动机的模板
    2017-04-07
    #include<cstdio>
    #include<cstring>
    #define newnode ++cnt
    #define m(s) memset(s,0,sizeof s);
    using namespace std;
    const int N=5e5+5;
    const int Sz=26;
    int n,T,root,cnt,ans;
    int q[N],fail[N];
    int tr[N][Sz],tag[N];
    char s[N<<1];bool mark[N];
    void insert(){
        int now=root,len=strlen(s);
        for(int i=0,t;i<len;i++){
            t=s[i]-'a';
            if(!tr[now][t]) tr[now][t]=newnode;
            now=tr[now][t];
        }
        tag[now]++;
    }
    void acmach(){
        for(int i=0;i<Sz;i++) tr[0][i]=root;
        int h=0,t=1;q[t]=root;fail[root]=0;
        while(h!=t){
            int now=q[++h];
            for(int i=0,p;i<Sz;i++){
                if(!tr[now][i]) continue;
                for(p=fail[now];!tr[p][i];p=fail[p]);
                p=tr[p][i];
                fail[tr[now][i]]=p;
                q[++t]=tr[now][i];
            }
        }
    }
    void solve(){
        int p=root,len=strlen(s);
        for(int i=0,t;i<len;i++){
            t=s[i]-'a';
            mark[p]=1;
            for(;!tr[p][t];p=fail[p]);
            p=tr[p][t];
            if(!mark[p]){
                for(int j=p;j;j=fail[j]){
                    ans+=tag[j];
                    tag[j]=0;
                }
            }
        }
        printf("%d
    ",ans);
    }
    void Clear(){
        ans=0;cnt=0;
        m(tr);m(tag);m(fail);m(mark);
    }
    int main(){
        for(scanf("%d",&T);Clear(),T--;){
            scanf("%d",&n);root=newnode;
            for(int i=1;i<=n;i++) scanf("%s",s),insert();
            acmach();
            scanf("%s",s);
            solve();
        }
        return 0;
    }
    /*
      tr[j][t]=i 表示j节点的第t个儿子是编号是i 
      j=fail[i] 表示在i节点之前输入的节点中找到与i的字母相同的节点j,
              并且j之前的字符一定与i之前的字符相吻合, 
              注意刚开始所有的fail[i]=0,到最后只有fail[1]=0;
      danger[i]表示以i节点结尾的的单词个数;
    值得注意的是,根节点的编号不能是0。因为kmp的fail[]会退到0
    */ #include<cstdio> #include<cstring> #include<algorithm> #define m(s) memset(s,0,sizeof s) using namespace std; const int N=5e5+10; int T,n,ans,cnt,q[N],fail[N],danger[N],tr[N][26]; char s1[51],s2[N<<1];bool mark[N]; void Cl(){ cnt=1;ans=0; m(fail);m(mark);m(danger);m(tr); for(int i=0;i<26;i++) tr[0][i]=1; } void ins(){//建树 int now=1,len=strlen(s1);//now=1是root for(int i=0;i<len;i++){ int t=s1[i]-'a'; if(!tr[now][t]) tr[now][t]=++cnt;//若没有编号,则生成编号为++cnt now=tr[now][t]; //如果当前节点有编号,将父亲的节点编号赋值为当前节点编号,以便于下个节点的使用 } danger[now]++;/*到每个单词的最后一个结点的时候,最后的now值便同最后一个节点的编号相等 因为最后一个节点此时已经没有儿子*/ } void acmach(){//在tire上搞kmp int h=0,t=1,now; q[1]=1;fail[1]=0;//root入队 while(h!=t){ now=q[++h]; for(int i=0;i<26;i++){ if(!tr[now][i]) continue;//找以now为父亲的节点 int p=fail[now];//找now的字母在先前出现过的节点为p while(!tr[p][i]) p=fail[p];//直到p的儿子与now的儿子字母相同 fail[tr[now][i]]=tr[p][i];//将now的儿子的与p的儿子编号匹配 q[++t]=tr[now][i];//将now的儿子入队 } } } void solve(){//查找s2字符串的模式串 int p=1,len=strlen(s2); for(int i=0;i<len;i++){ int t=s2[i]-'a'; mark[p]=1; while(!tr[p][t]) p=fail[p]; p=tr[p][t];//找到第一个可以进行匹配的节点 if(!mark[p]){ for(int j=p;j;j=fail[j]){ /*不断利用失败数组找可以进行匹配的节点,直到fail[j]=0*/ ans+=danger[j];//答案加上以该字母作为结束点的单词数 danger[j]=0; } } } printf("%d ",ans); } int main(){ for(scanf("%d",&T);Cl(),T--;){ scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%s",s1),ins(); acmach(); scanf("%s",s2); solve(); } return 0; }
  • 相关阅读:
    知识图谱概述
    架构浅谈之 MVC
    windows下安装Levenshtein
    python安装pyahocorasick遇到error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools":
    解决pycharm中: OSError: [WinError 1455] 页面文件太小,无法完成操作 的问题
    win10下yolov5的cpu和gpu环境搭建
    使用neo4j工具导入知识图谱
    【2021.03.07】看论文神器知云文献翻译、百度翻译API申请、机器学习术语库
    【2021.03.06】智能家居之双控开关改单控开关+无线开关
    【2021.03.06】IMAP协议与POP3协议的对比、在Gmail中添加QQ邮箱IMAP同步
  • 原文地址:https://www.cnblogs.com/shenben/p/5470163.html
Copyright © 2011-2022 走看看