题目描述

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

输入

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

输出

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

样例输入

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;
}

最新文章

  1. 【工匠大道】 svn命令自己总结
  2. Linux 下编译升级 Python
  3. JavaScript(一)
  4. 完数[HDU1406]
  5. Android之activity中新建控件
  6. linux screen 命令详解(未验证+研究)
  7. 如何将character_set_database latin1 改为 gbk(转)
  8. SpingMVC中利用BindingResult将错误信息返回到页面中
  9. git寻根——^和~的区别(转)
  10. flex盒子里面元素linehight对高度的影响
  11. 数据库系统概论——Chap. 1 Introduction
  12. React版本更新及升级须知(持续更新)
  13. 轻松理解AOP问题
  14. engine.go
  15. vector内存回收
  16. bzoj千题计划194:bzoj2115: [Wc2011] Xor
  17. 微信小程序——时间戳的转换及调用
  18. OpenCL 事件的使用,以及回调函数
  19. 批处理 进行svn代码拉取,vs编译,dotfuscator混淆
  20. 04_zookeeper的watcher机制

热门文章

  1. go语言,第三方包相对路径导入包引起的问题及解决方案(goquery)
  2. 浅谈前端性能优化(二)——对HTTP传输进行压缩
  3. 【洛谷4424】[HNOI/AHOI2018] 寻宝游戏(位运算思维题)
  4. 动态规划专题(三)——数位DP
  5. 欠采样(undersampling)和过采样(oversampling)会对模型带来怎样的影响
  6. python_20_列表
  7. pycharm 安装插件 支持markdown
  8. github相关问题
  9. Springboot端口设置
  10. JSP出现&quot;属性值[request.getParameter(&quot;myMessage&quot;)]引用[&quot;],在值内使用时必须被转义&quot;的解决方法