BZOJ4516: [Sdoi2016] Spell Spell

Description

The spell string consists of many spell characters, which can be represented by numbers. For example, the spell characters 1 and 2 can be pieced together to form a spell string [1,2].
A non-empty string of a spell string S is called the generating spell of the spell string S.
For example, when S=[1,2,1], its generation spells are [1], [2], [1,2], [2,1], [1,2,1]. When S=[1,1,1], its spawn spells are [1],
[1,1], [1,1,1] three. Initially S is the empty string. A total of n operations are performed, and each operation is to add a spell character at the end of S. after each operation
It is necessary to find out how many kinds of generated spells there are in the current spell string S.
 

Input

 

The first line contains an integer n.
The second line contains n numbers, and the ith number represents the magic character added by the ith operation.
1≤n≤100000. , the number x used to represent the spell character satisfies 1≤x≤10^9

Output

Output n lines, one number per line. The number in the ith row represents the number of generated spells of S after the ith operation

 

Sample Input

7
1 2 3 3 3 1 2

Sample Output

1
3
6
9
12
17
22
 
 
yy long live!
When I was learning SAM, I asked Rose to give me an example of what SAM can do. He said to find out how many different strings there are, just use the right set to add and subtract.
But this question has to run every time you add one, it's really troublesome, no, let's yy it!
The nature of SAM: for a node u, the different strings it can represent are Max(u)-Min(u)+1
In fact, it is Max(u)-(Min(u)-1)
Where Min(u)-1 is actually Max(parent(u))
why? ?
Because every path from root to parent is a suffix to u, it can come out yy
Let's take a look at the perceptual yy again. The result calculated for the current node is for the current point, such as
For the string AB, after adding B, ans is +2, and A has been calculated before, so the correctness can be guaranteed (I feel that I do SAM all by YY?)
Konjac must make mistakes in his speech, if there is something wrong, please correct me from all walks of life
 
code show as below:
#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct SAM{
    int dep,parent;
    map<int ,int > son;
}tr[210000];int last,root,cnt;
typedef long long ll;
ll ans=0ll;
void add(int x,int pos)
{
    int np++ cnt;
    tr[np].dep=pos;
    int p=last;
    while(p!=0&&tr[p].son[x]==0)tr[p].son[x]=np,p=tr[p].parent;
    if(p==0)tr[np].parent=root;
    else
    {
        int q=tr[p].son[x];
        if(tr[q].dep==tr[p].dep+1)tr[np].parent=q;
        else
        {
            int nq++ cnt;
            tr[nq] =tr[q];tr[nq].dep=tr[p].dep+ 1 ;
            tr[q].parent=tr[np].parent=nq;
            while(p!=0&&tr[p].son[x]==q)tr[p].son[x]=nq,p=tr[p].parent;
        }
    }
    last=np;
    ans +=(ll)tr[np].dep- tr[tr[np].parent].dep;
}
intmain ()
{
    int n;
    scanf("%d",&n);
    ans=0;root=last=++cnt;
    for(int i=1;i<=n;i++)
    {
        int x;scanf("%d",&x);
        add(x,i);
        printf("%lld\n",ans);
    }
    return 0;
}

by_lmy

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811946&siteId=291194637