题目描述
输入
输出
样例输入
10
10 9 8 7 6 5 4 3 2 1
样例输出
1 10 9 8 7 6 5 4 3 2
题解
后缀自动机+STL-map
表示并不会最小表示法,也不想学,于是用后缀自动机水了一发。
对于给定的原串,复制一遍后插入到后缀自动机中,然后再后缀自动机中找最小子串即可。
后缀自动机的具体实现过程与证明,可以参考:
陈老师PPT:https://wenku.baidu.com/view/fa02d3fff111f18582d05a81.html
大犇的blog:http://blog.sina.com.cn/s/blog_70811e1a01014dkz.html
看起来挺复杂,实际上代码真心短。
由于字符集过大,所以需要用map储存边集。
#include <cstdio>
#include <cstring>
#include <map>
#define N 1200010
using namespace std;
map<int , int> next[N];
map<int , int>::iterator it;
int a[N] , fa[N] , dis[N] , last = 1 , tot = 1;
void ins(int c)
{
int p = last , np = last = ++tot;
dis[np] = dis[p] + 1;
while(p && !next[p][c]) next[p][c] = np , p = fa[p];
if(!p) fa[np] = 1;
else
{
int q = next[p][c];
if(dis[q] == dis[p] + 1) fa[np] = q;
else
{
int nq = ++tot;
dis[nq] = dis[p] + 1 , next[nq] = next[q] , fa[nq] = fa[q] , fa[np] = fa[q] = nq;
while(p && next[p][c] == q) next[p][c] = nq , p = fa[p];
}
}
}
int main()
{
int n , i , p = 1;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &a[i]) , ins(a[i]);
for(i = 1 ; i < n ; i ++ ) ins(a[i]);
while(n -- )
{
it = next[p].begin();
printf("%d" , it->first);
if(n) printf(" ");
p = it->second;
}
return 0;
}
原文:http://www.cnblogs.com/GXZlegend/p/6950565.html