zoukankan      html  css  js  c++  java
  • bzoj4516 [Sdoi2016]生成魔咒

    Description

    魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示。例如可以将魔咒字符 1、2 拼凑起来形成一个魔咒串 [1,2]。
    一个魔咒串 S 的非空字串被称为魔咒串 S 的生成魔咒。
    例如 S=[1,2,1] 时,它的生成魔咒有 [1]、[2]、[1,2]、[2,1]、[1,2,1] 五种。S=[1,1,1] 时,它的生成魔咒有 [1]、
    [1,1]、[1,1,1] 三种。最初 S 为空串。共进行 n 次操作,每次操作是在 S 的结尾加入一个魔咒字符。每次操作后都
    需要求出,当前的魔咒串 S 共有多少种生成魔咒。

    Input

    第一行一个整数 n。
    第二行 n 个数,第 i 个数表示第 i 次操作加入的魔咒字符。
    1≤n≤100000。,用来表示魔咒字符的数字 x 满足 1≤x≤10^9

    Output

    输出 n 行,每行一个数。第 i 行的数表示第 i 次操作后 S 的生成魔咒数量 

    Sample Input

    7
    1 2 3 3 3 1 2

    Sample Output

    1
    3
    6
    9
    12
    17
    22

    正解:后缀自动机。

    因为题目是动态从前往后加字符,所以我们使用后缀自动机正好可以解决这个问题。

    然后每次增加字符以后,不同子串的增量就是当前状态到它父亲的状态的$len$的差。

     1 #include <bits/stdc++.h>
     2 #define il inline
     3 #define RG register
     4 #define ll long long
     5 #define N (200010)
     6 
     7 using namespace std;
     8 
     9 map<int,int> ch[N];
    10 
    11 int fa[N],l[N],n,la,tot;
    12 ll ans;
    13 
    14 il int gi(){
    15   RG int x=0,q=1; RG char ch=getchar();
    16   while ((ch<'0' || ch>'9') && ch!='-') ch=getchar();
    17   if (ch=='-') q=-1,ch=getchar();
    18   while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar();
    19   return q*x;
    20 }
    21 
    22 il void add(RG int c){
    23   RG int p=la,np=++tot; la=tot,l[np]=l[p]+1;
    24   for (;p && !ch[p][c];p=fa[p]) ch[p][c]=np;
    25   if (!p){ fa[np]=1,ans+=l[np]-l[fa[np]]; return; }
    26   RG int q=ch[p][c];
    27   if (l[q]==l[p]+1) fa[np]=q; else{
    28     RG int nq=++tot; l[nq]=l[p]+1;
    29     fa[nq]=fa[q],fa[q]=fa[np]=nq,ch[nq]=ch[q];
    30     for (;ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
    31   }
    32   ans+=l[np]-l[fa[np]]; return;
    33 }
    34 
    35 int main(){
    36 #ifndef ONLINE_JUDGE
    37   freopen("string.in","r",stdin);
    38   freopen("string.out","w",stdout);
    39 #endif
    40   n=gi(),tot=la=1;
    41   for (RG int i=1,c;i<=n;++i)
    42     c=gi(),add(c),printf("%lld
    ",ans);
    43   return 0;
    44 }
  • 相关阅读:
    hive同环比实现
    hive中的to_date和to_char
    正则表达式匹配一个独立的字符
    Mysql Explain用法详解
    hadoop安装踩坑
    hadoop ssh localhost无密码登录
    Node.js第十二篇:图片随机验证码
    Node.js第十一篇:Koa框架基础
    Ajax第五篇:JQuery中使用Ajax
    Ajax第四篇:跨域JSONP、CORS
  • 原文地址:https://www.cnblogs.com/wfj2048/p/7630229.html
Copyright © 2011-2022 走看看