zoukankan      html  css  js  c++  java
  • HDU2222 Keywords Search AC自动机

    Keywords Search

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 10744    Accepted Submission(s): 3722


    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
     
      该题为AC自动机模板题,果然它还是很强大的,该题要注意单词有重复出现的。
      代码如下:
      1 #include <cstring>
    2 #include <cstdio>
    3 #include <cstdlib>
    4 #include <queue>
    5 using namespace std;
    6
    7 struct Node
    8 {
    9 int end;
    10 Node *fail, *child[26];
    11 };
    12
    13 char s[1000005];
    14
    15 void getstr( char *s )
    16 {
    17 char c;
    18 int p = 0;
    19 while( c = getchar(), c == ' ' || c == '\n' ) ;
    20 s[p++] = c;
    21 while( c = getchar(), c != ' ' && c != '\n' )
    22 s[p++] = c;
    23 s[p] = '\0';
    24 }
    25
    26 Node *init( )
    27 {
    28 Node *r = ( Node * )malloc( sizeof( Node ) );
    29 r->end = 0;
    30 r->fail = NULL;
    31 memset( r->child, NULL, sizeof( r->child ) );
    32 return r;
    33 }
    34
    35 void creattree( Node *p, char *in )
    36 {
    37 int index = *in - 'a';
    38 if( p->child[index] == NULL )
    39 p->child[index] = init();
    40 if( *( in + 1 ) )
    41 creattree( p->child[index], in + 1 );
    42 else
    43 p->child[index]->end++;
    44 }
    45
    46 void creatfail( Node *p )
    47 {
    48 queue< Node * >q;
    49 q.push( p );
    50 while( !q.empty() )
    51 {
    52 Node *pos = q.front();
    53 q.pop();
    54 for( int i = 0; i < 26; ++i )
    55 {
    56 Node *f = pos->fail;
    57 if( pos->child[i] )
    58 {
    59 while( f != NULL )
    60 {
    61 if( f->child[i] )
    62 {
    63 pos->child[i]->fail = f->child[i];
    64 break;
    65 }
    66 else
    67 f = f->fail;
    68 }
    69 if( f == NULL )
    70 {
    71 pos->child[i]->fail = p;
    72 }
    73 q.push( pos->child[i] );
    74 }
    75 }
    76 }
    77 }
    78
    79 int acauto( Node *p, char *text )
    80 {
    81 int ans = 0, lenth = 0, len = strlen( text );
    82 Node *f = p;
    83 for( int i = 0; i < len; ++i )
    84 {
    85 int type = text[i] - 'a';
    86 while( f )
    87 {
    88 if( f->child[type] )
    89 {
    90 f = f->child[type];
    91 break;
    92 }
    93 else
    94 f = f->fail;
    95 }
    96
    97 if( f == NULL ) // 如果都已经找到了根节点仍不匹配
    98 {
    99 f = p; // 从头开始
    100 continue;
    101 }
    102
    103 Node *t = f;
    104 while( t != p ) // 成功匹配一个字符的更新操作
    105 {
    106 if( t->end )
    107 {
    108 ans += t->end;
    109 t->end = 0;
    110 }
    111 t = t->fail;
    112 }
    113 }
    114 return ans;
    115 }
    116
    117 void _free( Node *p )
    118 {
    119 for( int i = 0; i < 26; ++i )
    120 {
    121 if( p->child[i] )
    122 _free( p->child[i] );
    123 }
    124 free( p );
    125 }
    126
    127 int main()
    128 {
    129 int T;
    130 scanf( "%d\n", &T );
    131 while( T-- )
    132 {
    133 int N;
    134 Node *p = init();
    135 scanf( "%d", &N );
    136 for( int i = 0; i < N; ++i )
    137 {
    138 getstr( s );
    139 creattree( p, s );
    140 }
    141 getstr( s );
    142 creatfail( p );
    143 printf( "%d\n", acauto( p, s ) );
    144 }
    145 }

      

  • 相关阅读:
    C++中static修饰的静态成员函数、静态数据成员
    C++友元函数、友元类
    C++异常处理
    运行时类型识别RTTI
    AD转换
    敏捷模式下的测试用例该如何存在?
    使用Postman轻松实现接口数据关联
    接口测试Mock利器-moco runner
    python测开平台使用dockerfile构建镜像
    MySQL – 用SHOW STATUS 查看MySQL服务器状态
  • 原文地址:https://www.cnblogs.com/Lyush/p/2165975.html
Copyright © 2011-2022 走看看