病毒侵袭(hdu

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5-20;
int n;
int TOT=0;
namespace AC {
    int tr[N][130], tot;set<int> ans;
    int fail[N],end[N];
    void insert(char *s,int id) {
        int u = 0;
        for (int i = 1; s[i]; i++) {
            if (!tr[u][s[i]]) tr[u][s[i]] = ++tot;
            u = tr[u][s[i]];
        }
        end[u]=id;
        // v[u].push_back(id);
    }
    queue<int> q;
    void build() {
        for (int i = 0; i < 129; i++)
            if (tr[0][i]) q.push(tr[0][i]);
        while (q.size()) {
            int u = q.front();
            q.pop();
            for (int i = 0; i < 129; i++) {
                if (tr[u][i])
                    fail[tr[u][i]] = tr[fail[u]][i], q.push(tr[u][i]);
                else
                    tr[u][i] = tr[fail[u]][i];
            }
        }
    }
    int query(char *t,int id) {
        int u = 0, res = 0;ans.clear();
        for (int i = 1; t[i]; i++) {
            u = tr[u][t[i]];  // 转移
            for (int j = u; j ; j = fail[j]) {
                if(end[j]) ans.insert(end[j]);
            }
        }
        if(ans.size()){
            printf("web %d:",id);TOT++;
            for(auto it:ans){printf(" %d",(it));}
            printf("\n");
        }
        return res;
    }
    void init(){
        memset(end,0,sizeof end);
        memset(fail,0,sizeof fail);
        memset(tr,0,sizeof tr);
        tot=0;
    }
}  // namespace AC


char s[N];int T,m;
int main() {
   T=1;
   while(T--){
        scanf("%d", &n);
        for (int i = 1; i <= n; i++) scanf("%s", s + 1), AC::insert(s,i);   
        AC::build();
        scanf("%d",&m);
        for(int i=1;i<=m;i++){
            scanf("%s", s + 1);
            AC::query(s,i);
        }
        printf("total: %d\n",TOT);
        // printf("%d\n", );
        // return 0;
   }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值