zoukankan      html  css  js  c++  java
  • BZOJ2882:工艺(SAM)

    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

    Solution

    将串S连接成SS后建立SAM,每次找最小的儿子跑就好了
    涨了点stl的姿势……QAQ

    Code

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<map>
     5 #define N (1200000+1000)
     6 using namespace std;
     7 
     8 int n,a[N>>2];
     9 
    10 struct SAM
    11 {
    12     map<int,int>son[N];
    13     map<int,int>::iterator iter;
    14     int fa[N],step[N];
    15     int p,q,np,nq,last,cnt;
    16     SAM(){last=++cnt;}
    17     
    18     void Insert(int x)
    19     {
    20         p=last; np=last=++cnt; step[np]=step[p]+1;
    21         while (p && !son[p][x]) son[p][x]=np,p=fa[p];
    22         if (!p) fa[np]=1;
    23         else
    24         {
    25             q=son[p][x];
    26             if (step[p]+1==step[q]) fa[np]=q;
    27             else
    28             {
    29                 nq=++cnt; step[nq]=step[p]+1;
    30                 son[nq]=son[q];
    31                 fa[nq]=fa[q]; fa[q]=fa[np]=nq;
    32                 while (son[p][x]==q) son[p][x]=nq,p=fa[p];
    33             }
    34         }
    35     }
    36     void Solve()
    37     {
    38         int now=1;
    39         for (int i=1; i<=n; ++i)
    40         {
    41             iter=son[now].begin();
    42             printf("%d ",(*iter).first);
    43             now=(*iter).second;
    44         }
    45     }
    46 }SAM;
    47 
    48 int main()
    49 {
    50     scanf("%d",&n);
    51     for (int i=1; i<=n; ++i)
    52         scanf("%d",&a[i]);
    53     for (int i=1; i<=n; ++i)
    54         SAM.Insert(a[i]);
    55     for (int i=1; i<=n; ++i)
    56         SAM.Insert(a[i]);
    57     SAM.Solve();
    58 }
  • 相关阅读:
    oracle中xhost报错
    cronolog切割apache和tomcat日志
    rsync配置和同步数据
    Jenkins+GitHub+maven
    Git只获取部分目录的内容
    git命令综合
    tomcat(不仅仅是tomcat)通过熵池解决在linux启动应用慢
    iptables之ipset集群工具
    Python中yield表达式的使用
    对于python中出现UnicodeDecodeError问题的解决方案
  • 原文地址:https://www.cnblogs.com/refun/p/9370466.html
Copyright © 2011-2022 走看看