BZOJ 2882: 工艺( 后缀自动机 )

把串S复制成SS然后扔进后缀自动机里, 从根选最小的儿子走, 走N步就是答案了...一开始还想写个treap的...后来觉得太麻烦..就用map了...

---------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
 
using namespace std;
 
const int maxn = 300009;
 
struct Node {
Node* fa;
map<int, Node*> ch;
int len;
} pool[maxn << 2], *pt = pool, *root, *last;
 
Node* newNode(int v) {
pt->fa = NULL;
pt->ch.clear();
pt->len = v;
return pt++;
}
 
void init() {
pt = pool;
root = last = newNode(0);
}
 
void Extend(int c) {
Node *p = last, *np = newNode(p->len + 1);
for(; p && !p->ch[c]; p = p->fa)
p->ch[c] = np;
if(!p)
np->fa = root;
else {
Node* q = p->ch[c];
if(p->len + 1 == q->len)
np->fa = q;
else {
Node* nq = newNode(p->len + 1);
nq->fa = q->fa;
q->fa = np->fa = nq;
for(map<int, Node*>::iterator it = q->ch.begin(); it != q->ch.end(); it++)
nq->ch[it->first] = it->second;
for(; p && p->ch[c] == q; p = p->fa)
p->ch[c] = nq;
}
}
last = np;
}
 
int N, num[maxn];
 
int main() {
init();
scanf("%d", &N);
for(int i = 0; i < N; i++)
scanf("%d", num + i);
for(int i = 0; i < 2; i++)
for(int j = 0; j < N; j++)
Extend(num[j]);
for(int i = 0; i < N; i++) {
map<int, Node*>::iterator it = root->ch.begin();
if(i) putchar(' ');
printf("%d", it->first);
root = it->second;
}
return 0;
}

---------------------------------------------------------------------------

2882: 工艺

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 268  Solved: 108
[Submit][Status][Discuss]

Description

小敏和小燕是一对好朋友。
他们正在玩一种神奇的游戏,叫Minecraft。
他们现在要做一个由方块构成的长条工艺品。但是方块现在是乱的,而且由于机器的要求,他们只能做到把这个工艺品最左边的方块放到最右边。
他们想,在仅这一个操作下,最漂亮的工艺品能多漂亮。
两个工艺品美观的比较方法是,从头开始比较,如果第i个位置上方块不一样那么谁的瑕疵度小,那么谁就更漂亮,如果一样那么继续比较第i+1个方块。如果全都一样,那么这两个工艺品就一样漂亮。

Input

第一行两个整数n,代表方块的数目。
第二行n个整数,每个整数按从左到右的顺序输出方块瑕疵度的值。

Output

一行n个整数,代表最美观工艺品从左到右瑕疵度的值。

Sample Input


10
10 9 8 7 6 5 4 3 2 1

Sample Output


1 10 9 8 7 6 5 4 3 2

HINT



【数据规模与约定】

对于20%的数据,n<=1000

对于40%的数据,n<=10000

对于100%的数据,n<=300000

Source

原文地址:https://www.cnblogs.com/JSZX11556/p/4969770.html