2882: 工艺

显然看到题第一反应sam /sa瞎搞搞。。。
然后看到数据范围,发现值域过大…
其实也无所谓…套个map,多个log搞定。。。
当然其实可以用 最小表示法 (O(n)). (我不会orz)
c++代码如下:

#include<bits/stdc++.h>
#define rep(i,x,y) for(register int i = x ; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x ; i >= y; -- i)
using namespace std;
typedef long long ll;
template<typename T>inline void read(T&x)
{
    x = 0;char c;int sign = 1;
    do { c = getchar(); if(c == '-') sign = -1; }while(!isdigit(c));
    do { x = x * 10 + c - '0'; c = getchar(); }while(isdigit(c));
    x *= sign;
}

const int N = 3e5+50;
struct SAM
{
    map<int,int> ch;
    int len,pre;
}t[4*N];
int n,root = 1,sz = 1,lst = 1,np,a[N];

inline void insert(int x)
{
    int np = ++ sz,p = lst;
    t[np].len = t[p].len + 1;
    for(;p && !t[p].ch[x];p = t[p].pre) t[p].ch[x] = np;
    if(!p) t[np].pre = root;
    else
    {
        int q = t[p].ch[x];
        if(t[p].len + 1 == t[q].len) t[np].pre = q;
        else
        {
            int nq = ++ sz;
            t[nq] = t[q]; t[nq].len = t[p].len + 1;
            t[np].pre = t[q].pre = nq;
            for(;p && t[p].ch[x] == q; p = t[p].pre) t[p].ch[x] = nq;
        }
    }
    lst = np;
}

int main()
{
    read(n);
    rep(i,1,n)
    {
        read(a[i]);
        insert(a[i]);
    }

    rep(i,1,n) insert(a[i]);
    int now = 1;
    rep(i,1,n)
    {
        map<int,int>::iterator it;
        it = t[now].ch.begin();
        printf("%d ",(*it).first);
        now = (*it).second;
    }

    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值