码迷,mamicode.com
首页 > 其他好文 > 详细

[Sdoi2016]生成魔咒[SAM or SA]

时间:2017-05-05 21:50:01      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ems   href   etc   memory   个数   script   lld   字符   name   

4516: [Sdoi2016]生成魔咒

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1017  Solved: 569
[Submit][Status][Discuss]

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

HINT

 

Source

#include<map>
#include<cstdio>
using namespace std;
typedef long long ll;
template <typename T>
inline void read(T &x){
    register char ch=getchar();x=0;
    while(ch<0||ch>9) ch=getchar();
    while(ch>=0&&ch<=9){x=x*10+ch-0;ch=getchar();}
}
const int N=2e5+5;
int n,p,q,np,nq;ll ans;
int last,cnt;
int fa[N],l[N];map<int,int>a[N];
inline void extend(int c){
    p=last;np=last=++cnt;l[np]=l[p]+1;
    while(!a[p][c]&&p) a[p][c]=np,p=fa[p];
    if(!p) fa[np]=1;
    else{
        q=a[p][c];
        if(l[p]+1==l[q]) fa[np]=q;
        else{
            nq=++cnt;l[nq]=l[p]+1;
            a[nq]=a[q];
            fa[nq]=fa[q];
            fa[np]=fa[q]=nq;
            while(a[p][c]==q) a[p][c]=nq,p=fa[p];
        }
    }
    ans+=l[np]-l[fa[np]];
    printf("%lld\n",ans);
}
int main(){
    read(n);last=++cnt;
    for(int i=1,x;i<=n;i++) read(x),extend(x);
    return 0;
}

 SA+双向链表:见这里

[Sdoi2016]生成魔咒[SAM or SA]

标签:ems   href   etc   memory   个数   script   lld   字符   name   

原文地址:http://www.cnblogs.com/shenben/p/6814842.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!