zoukankan      html  css  js  c++  java
  • HDU2896+AC自动机

    ac自动机 模板题

      1 /*
      2 
      3 */
      4 #include<stdio.h>
      5 #include<string.h>
      6 #include<stdlib.h>
      7 #include<algorithm>
      8 #include<iostream>
      9 #include<queue>
     10 #include<map>
     11 #include<stack>
     12 #include<set>
     13 #include<math.h>
     14 using namespace std;
     15 typedef long long int64;
     16 //typedef __int64 int64;
     17 typedef pair<int64,int64> PII;
     18 #define MP(a,b) make_pair((a),(b)) 
     19 const int maxn = 100;
     20 const int maxm = 10005;
     21 const int inf = 0x7fffffff;
     22 const double pi=acos(-1.0);
     23 const double eps = 1e-8;
     24 
     25 struct Tree{
     26     int id;
     27     Tree *fail;
     28     Tree *next[ maxn ];
     29 };
     30 Tree root;
     31 Tree *q[ 20000005 ];
     32 char str[ maxm ];
     33 int tot;
     34 int cnt[ 555 ];
     35 int Index_cnt ;
     36 bool vis[ 555 ];
     37 
     38 void init(){
     39     root.id = 0;
     40     root.fail = NULL;
     41     for( int i=0;i<maxn;i++ )
     42         root.next[ i ] = NULL;
     43 }
     44 
     45 void build( char str[],int ID ){
     46     int len = strlen( str );
     47     Tree *p = &root;
     48     Tree *tmp = NULL;
     49     for( int i=0;i<len;i++ ){
     50         int id = str[i]-32;
     51         if( p->next[ id ]==NULL ){
     52             tmp = (Tree *)malloc(sizeof( root ));
     53             tmp->id = 0;
     54             for( int j=0;j<maxn;j++ )
     55                 tmp->next[ j ] = NULL;
     56             p->next[ id ] = tmp;
     57             p = p->next[ id ];
     58         }
     59         else 
     60             p = p->next[ id ];
     61     }
     62     p->id = ID;
     63     return ;
     64 }
     65 
     66 void build_AC(){
     67     int head = 0,tail = 0;
     68     Tree *p = &root;
     69     Tree *tmp = NULL;
     70     q[ tail++ ] = p;
     71     while( head!=tail ){
     72         p = q[ head++ ];
     73         for( int i=0;i<maxn;i++ ){
     74             if( p->next[i]!=NULL ){
     75                 if( p==(&root) ){
     76                     p->next[i]->fail = &root;
     77                 }
     78                 else{
     79                     tmp = p->fail;
     80                     while( tmp!=NULL ){
     81                         if( tmp->next[i]!=NULL ){
     82                             p->next[i]->fail = tmp->next[i];
     83                             break;
     84                         }
     85                         tmp = tmp->fail;
     86                     }
     87                     if( tmp==NULL )
     88                         p->next[i]->fail = &root;
     89                 }
     90                 q[ tail++ ] = p->next[i];
     91             }
     92         }
     93     }
     94 }
     95 
     96 void query( char str[] ){
     97     int len = strlen( str );
     98     Tree *p = &root;
     99     Tree *tmp = NULL;
    100     for( int i=0;i<len;i++ ){
    101         int id = str[i]-32;
    102         while( p->next[id]==NULL&&p!=(&root) ){
    103             p = p->fail;
    104         }
    105         p = p->next[ id ];
    106         if( p==NULL )
    107             p = &root;
    108         tmp = p;
    109         while( tmp!=(&root) ){
    110             if( tmp->id!=0&&vis[tmp->id]==false ){
    111                 vis[tmp->id] = true;
    112                 cnt[ Index_cnt++ ] = tmp->id;
    113             }
    114             tmp = tmp->fail;
    115         }
    116     }
    117     return ;
    118 }
    119 
    120 int main(){
    121     int n,m;
    122     while( scanf("%d",&n)!=EOF ){
    123         init();
    124         char s[ 255 ];
    125         for( int i=1;i<=n;i++ ){
    126             scanf("%s",s);
    127             build( s,i );
    128         }
    129         build_AC();
    130         scanf("%d",&m);
    131         tot = 0;
    132         for( int i=1;i<=m;i++ ){
    133             scanf("%s",str);
    134             Index_cnt = 0;
    135             memset( vis,false,sizeof( vis ) );
    136             query( str );
    137             if( Index_cnt!=0 ) {
    138                 tot++;sort( cnt,cnt+Index_cnt );
    139                 printf("web %d:",i);
    140                 for( int j=0;j<Index_cnt;j++ )
    141                     printf(" %d",cnt[j]);
    142                 printf("
    ");
    143             }
    144         }
    145         printf("total: %d
    ",tot);
    146     }
    147     return 0;
    148 }
    View Code
    keep moving...
  • 相关阅读:
    死锁程序示例
    用Intellij打可执行jar包
    Semaphore tryAcquire release 正确的使用方法
    计算对象占用空间工具类
    mysql高效分页方案及原理
    乐视秒杀:每秒十万笔交易的数据架构解读
    mysql 联合索引(转)
    mysql中in和exists二者的区别和性能影响
    怎样避免 i f 判断过多,全复杂度较高,代码不美观的问题?
    Java中Enum类型的序列化(转)
  • 原文地址:https://www.cnblogs.com/xxx0624/p/3281634.html
Copyright © 2011-2022 走看看