bzoj 2882: 工艺 后缀自动机

题目大意:

http://www.lydsy.com/JudgeOnline/problem.php?id=2882

题解:

让我想起了我的Minecraft世界

我们把这个串倍增一下接在后面,然后构建后缀自动机
然后跑n步,每一步都选择当前最小的节点走即可.

#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 300010; 
struct Node{
    int len,fa;
    map<int,int>nx;
}T[maxn<<2];
int last,nodecnt;
inline void init(){
    last = nodecnt = 0;
    T[0].len = 0;T[0].fa = -1;
}
void insert(int c){
    int cur = ++nodecnt,p;
    T[cur].len = T[last].len + 1;
    for(p=last;p!=-1 && !T[p].nx.count(c);p = T[p].fa)
        T[p].nx[c] = cur;
    if(p == -1) T[cur].fa = 0;
    else{
        int q = T[p].nx[c];
        if(T[q].len == T[p].len + 1) T[cur].fa = q;
        else{
            int co = ++ nodecnt;
            T[co] = T[q];T[co].len = T[p].len+1;
            for(;p!=-1 && T[p].nx[c] == q;p = T[p].fa)
                T[p].nx[c] = co;
            T[q].fa = T[cur].fa = co;
        }
    }last = cur;
}
int a[maxn];
int main(){
    int n;read(n);init();
    for(int i=1;i<=n;++i) read(a[i]),insert(a[i]);
    for(int i=1;i<=n;++i) insert(a[i]);
    int nw = 0;
    while(n--){
        int pos = T[nw].nx.begin()->first;
        printf("%d",pos);nw = T[nw].nx[pos];
        if(n != 0) putchar(' ');
        else putchar('\n');
    }
    getchar();getchar();
    return 0;
}

转载于:https://www.cnblogs.com/Skyminer/p/6522892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值