HDU 2896

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 100008;
int   sz, ans[8];
int   arr[maxn], mark[maxn];
char  ch[508][208];
char  txt[10008];

struct Node{
      int  count, fail, next[94];
      void init(){
           memset(next,  0, sizeof(next));
           count = fail = 0;
      }
}node[maxn];

void insert(char *s, int id){
     int p = 0;
     while(*s){
         int c = *s++ - ' ';
         if(!node[p].next[c]){
             node[sz].init();
             node[p].next[c] = sz++;
         }
         p = node[p].next[c];
     }
     node[p].count = id;
}
void build_ac(){
     int head = 0, rear = 0, p, fail;
     for(int i = 0; i < 94; i++) if(node[0].next[i]){
          arr[rear++] = node[0].next[i];
     }
     while(head < rear){
          int now = arr[head++];
          int fail = node[now].fail;
          for(int i = 0; i < 94; i++){
               if(p = node[now].next[i]){
                   arr[rear++] = p;
                   node[p].fail = node[fail].next[i];
               }else node[now].next[i] = node[fail].next[i];
          }
     }
}
int find(char *s){
    int p = 0, cnt = 0;
    fill(mark, mark + sz, 0);
    while(*s){
         int c = *s++ - ' ';
         p = node[p].next[c];
         int tmp = p;
         while(tmp && !mark[tmp]){
              mark[tmp] = 1;
              if(node[tmp].count) ans[++cnt] = node[tmp].count;
              tmp = node[tmp].fail;
         }
    }
    return cnt;
}
int main(){
       int  n,  m;
       // freopen("hdu 2896.in", "r", stdin);
       while(scanf("%d", &n) != EOF){
           sz = 1;  node[0].init();
           for(int i = 1; i <= n; i++){
               scanf("%s", ch[i]);
               insert(ch[i], i);
           }
           build_ac();
           scanf("%d",&m);
           int tot = 0, cnt;
           for(int i = 1; i <= m; i++){
                scanf("%s", txt);
                cnt = find(txt);
                if(cnt) {
                    tot++;
                    sort(ans + 1, ans + cnt + 1);
                    printf("web %d: ",i);
                    for(int j = 1; j < cnt; j++) printf("%d ", ans[j]);
                    printf("%d\n", ans[cnt]);
                }
           }
           printf("total: %d\n", tot);
       }
       //system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值