HDU 6138 Fleet of the Eternal Throne

AC自动机。所有的串都插入字典树跑失配。结点val值标为字典树上的深度。
对于每对串,前一个串先跑一遍匹配把点都标记下,后一个串跑一下去标记过的最深点就可以了。

代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 1e5+5;
const int MAXNODE = N<<2, SIGMA_SIZE = 26;
#define CLR(A, X) memset(A, X, sizeof(A))

struct AC {
    int ch[MAXNODE][SIGMA_SIZE], f[MAXNODE], val[MAXNODE], last[MAXNODE], sz;
    int vis[MAXNODE];

    int idx(char ch) { return ch-'a'; }

    void init() { sz = 1; val[0] = 0; CLR(ch[0], 0); CLR(vis, 0); }

    int Insert(char *s) {
        int u = 0, n = strlen(s);
        for(int i = 0; i < n; i++) {
            int c = idx(s[i]);
            if(!ch[u][c]) {
                CLR(ch[sz], 0);
                val[sz] = val[u]+1;
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }
        return u;
    }

    int Find(char* T, int op, int x) {
        int n = strlen(T), ret = 0;
        for(int i = 0, j = 0; i < n; i++) {
            int c = idx(T[i]);
            int t = j = ch[j][c];
            while(t) {
                if(op == 1) vis[t] = x;
                else if(vis[t] == x) ret = max(ret, val[t]);
                t = last[t];
            }
        }
        return ret;
    }

    void getFail() {
        queue<int> Q;
        f[0] = 0;
        for(int c = 0; c < SIGMA_SIZE; c++) {
            int u = ch[0][c];
            if(u) { f[u] = 0; Q.push(u); last[u] = 0; }
        }
        while(!Q.empty()) {
            int r = Q.front(); Q.pop();
            for(int c = 0; c < SIGMA_SIZE; c++) {
                int u = ch[r][c];
                if(!u) { ch[r][c] = ch[f[r]][c]; continue; }
                Q.push(u);
                int v = f[r];
                while(v && !ch[v][c]) v = f[v];
                f[u] = ch[v][c];
                last[u] = val[f[u]] ? f[u] : last[f[u]];
            }
        }
    }
}A;



char *str[N], s[N];

int main() {
    int T, n, m;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        A.init();
        for(int i = 0, j = 0; i < n; i++) {
            scanf("%s", s+j);
            str[i] = s+j;
            A.Insert(s+j);
            j = j+strlen(s+j)+1;
        }
        A.getFail();
        scanf("%d", &m);
        while(m--) {
            int u, v;
            scanf("%d%d", &u, &v);
            u--, v--;
            A.Find(str[u], 1, m+1);
            printf("%d\n", A.Find(str[v], 2, m+1));
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值