hdu 病毒入侵

#include<map>
#include<queue>
#include<stdint.h>
#include<stdio.h>
#include<string.h>
#include<bits/stdc++.h>
using namespace std;
#include<iostream>
#pragma comment(linker,"/STACK:1024000000,1024000000")
const int maxn = 1e5;
char  str[10010];
int flag[505*2];
int biao;
struct  node
{
    int next[maxn][130];
    int  end[maxn];
    int fail[maxn];
    int root;
    int L ;
    void init()
    {
        L = 0 ;

        root = newnode();
    }
    int newnode()
    {
        for(int i = 0; i<128; i++)
        {
            next[L][i] = -1;
        }
        end[L++]=0 ;
        return L -1;
    }
    void insert(int num)
    {
        int now = root;
        int len = strlen(str);
        for(int i = 0; i<len; i++)
        {
            if(next[now][str[i]]==-1)
            {
                next[now][str[i]] = newnode();

            }
            now = next[now][str[i]];
        }
        end[now] = num;
        //printf("he");
    }
    void getfali()
    {
        queue<int>q;
        fail[root] = root;
        for(int i = 0 ; i<128; i++)
        {
            if(next[root][i]==-1)
            {

                next[root][i] =root;
            }
            else
            {
                fail[next[root][i]] = root ;
                q.push(next[root][i]);
            }
        }
        while(!q.empty())
        { //  printf("hihihi");
            int now = q.front();
            q.pop();
            for(int i = 0 ; i<128; i++)
            {
                if( next[now][i]==-1)
                {
                    next[now][i] =next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]]= next[fail[now]][i];
                    q.push(next[now][i]);
                }
            }
        }
    }
    void   search()
    {
        int len = strlen(str);
       // puts("s");
        //puts(str);
        int now = root ;
        for(int i = 0; i<len; i++)
        {
            now = next[now][str[i]];
            int temp = now ;
            while(temp!=root)
            {
                if(end[temp]>0)
                {
                 //   printf("agaag");
                    flag[end[temp]] = 1;
                    biao = 1;
                }
                temp  = fail[temp];
            }
        }
    }
};
node ac;
int main()
{

    int n,m;
    while(scanf("%d",&n)!= EOF)
    {   getchar();
          ac.init();
        for(int i = 1; i<= n; i++)
        {
           gets(str);
            ac.insert( i );

        }
        ac.getfali();
        scanf("%d",&m);
        getchar();
    //    puts("hihi");
     int ans = 0;
        for(int i = 1; i<= m; i++)
        {
            memset(flag,0,sizeof(flag));
            biao = 0;
            gets(str);
           // getchar();
            ac.search();
            if(biao)
            {    ans++;
                      printf("web %d:",i);
                      int x;
                      for(x = 1;x<=n;x++)
                      {
                          if(flag[x])
                          {
                              printf(" %d",x);
                           //  break;
                          }
                      }
                   putchar('\n');
            }

        }
         printf("total: %d\n",ans);
    }
    return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值