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;
}
posted @ 2017-03-08 20:30  Sky_miner  阅读(198)  评论(0编辑  收藏  举报