zoukankan      html  css  js  c++  java
  • AC自动机 HDU 2222 Keywords 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
     
     
                         http://yzmduncan.iteye.com/blog/1217014
     
    模板:
    View Code
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 int const N= 500010;
     5 struct Trie{
     6     int flag;  // 标记是否为某一模式串的结尾 
     7     int fail;  // 失败指针 
     8     int next[26];
     9     void init(){
    10         flag= 0; fail= -1;
    11         for( int i= 0; i< 26; ++i ) next[i]= 0; }
    12 }tb[N];
    13 
    14 int cnt= 0, que[N], n;
    15 char str[1000010];
    16 
    17 void inline insert( char* s ){
    18     int rt= 0;
    19     while( *s ){
    20         int t= *s- 'a';
    21         if( !tb[rt].next[t] ){
    22             tb[++cnt].init();
    23             tb[rt].next[t]= cnt;
    24         }
    25         rt= tb[rt].next[t]; s++;
    26     }
    27     tb[rt].flag++;
    28 }
    29 
    30 void bfs(){
    31     int head= 0, tail= 0, p, q;
    32     que[0]= 0;
    33     while( head<= tail ){
    34         int now= que[head++];
    35         for( int t= 0; t< 26; ++t )
    36         if( tb[now].next[t] ){
    37             p= tb[now].fail, q= tb[now].next[t];
    38             while( p!= -1 && !tb[p].next[t] ) p= tb[p].fail;
    39             if( p== -1 ) tb[q].fail= 0;
    40             else tb[q].fail= tb[p].next[t];
    41             que[++tail]= q;
    42         }
    43     }
    44 }
    45 
    46 void Match( char* s ){
    47     int ans= 0, rt= 0, t, p;
    48     while( *s ){
    49         t= *s- 'a';
    50         if( tb[rt].next[t] ) rt= tb[rt].next[t];
    51         else{
    52             p= tb[rt].fail;
    53             while( p!= -1 && !tb[p].next[t] ) p= tb[p].fail;
    54             if( p== -1 ) rt= 0;
    55             else rt= tb[p].next[t];
    56         }
    57         p= rt;
    58         while( p!= 0 && tb[p].flag ){
    59             if( tb[p].flag ){
    60                 ans+= tb[p].flag; tb[p].flag= 0; }
    61             p= tb[p].fail;
    62         }
    63         s++;
    64     }
    65     printf("%d\n", ans );
    66 }
    67 
    68 int main(){
    69     int test;
    70     scanf("%d",&test ); 
    71     while( test-- ){
    72         scanf("%d\n",&n ); 
    73         cnt= 0; tb[0].init();
    74         while( n-- ){
    75             gets(str);
    76             insert( str );
    77         }
    78         bfs();
    79         gets(str);
    80         Match( str );
    81     }
    82     
    83     return 0;
    84 }
  • 相关阅读:
    loadNibNamed 的使用
    重写UIPageControl实现自定义按钮(转)
    乔布斯办公室语录
    学ACM算法题有用吗?
    基于文法分析的表达式计算器的实现
    我的程序员之路(5)——工作一年
    XCode实用快捷键,谁用谁知道
    LR(1)语法分析表生成
    九大定律,四大原则
    汉字为何不能用笔画编码信息论系列
  • 原文地址:https://www.cnblogs.com/frog112111/p/2742564.html
Copyright © 2011-2022 走看看