• 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('
    ');
        }
        getchar();getchar();
        return 0;
    }
    
  • 相关阅读:
    Java 堆和栈 垃圾回收 2015/9/16
    多态 Java 2015/9/16
    学校项目过程中知识点 Java 2015/9/15 晚
    Python print输出不换行
    Windows 加载EXT分区
    RouterOS 安全模式
    RouterOS Openswan l2tp ipsec
    RouterOS 自动邮件备份脚本
    python chnroutes ROS版
    Kinect 2.0&Speech 11 中文语音控制
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6522892.html
Copyright © 2020-2023  润新知