BZOJ4516: [Sdoi2016] generate spell suffix automaton

Source: Internet
Author: User

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <cmath > #include <algorithm> #include <cstdlib> #include <map> #define N 200005#define ll Long longusing    namespace Std;int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;}  int fa[n],mx[n];int rt,lst,tot;map<int,int> son[n];int n;ll ans=0;void ins (int x) {int p=lst,np=++tot;  mx[np]=mx[p]+1;  while (P&&!son[p][x]) {son[p][x]=np;p=fa[p];  } if (!p) Fa[np]=rt;    else{int q=son[p][x];    if (mx[q]==mx[p]+1) fa[np]=q;        else{int Nq=++tot;        mx[nq]=mx[p]+1;        SON[NQ]=SON[Q];        FA[NQ]=FA[Q];        FA[NP]=FA[Q]=NQ;        while (son[p][x]==q&&p) {son[p][x]=nq;p=fa[p];  }}} LST=NP;  ANS+=MX[NP]-MX[FA[NP]]; printf ("%lld\n", ans);}    int main () {n=read ();   Rt=tot=lst=1; for (int i=1;i<=n;i++) {int x=read ();    Ins (x); } return 0;}
4516: [Sdoi2016] Generate magic Spell time limit:10 Sec Memory limit:128 MB
submit:459 solved:282
[Submit] [Status] [Discuss] The description charm string consists of a number of spell characters, which can be represented by numbers. For example, you can piece together the spell character 1 and 2 to form a magic string. The non-empty string of a spell string s is called the Spawn enchantment of the spell string s. For example s=[1,2,1], its spawn enchantments are [1], [2], [up], [2,1], [1,2,1] five. s=[1,1,1], its spawn spells are [1], [three], [1,1,1]. Initially S is an empty string. A total of n operations, each of which adds a spell character at the end of S. After each operation, we need to find out how many kinds of enchantments the current spell string S has. Input

The first line is an integer n. The number of the second row N, the number of I indicates the spell character added by the operation of the first. 1≤n≤100000. , the digit x that is used to denote the spell character meets 1≤x≤10^9output

Output n rows, one number per line. Number of line I indicates the number of enchantments generated by S after the first operation.

Sample Input7
1 2 3 3 3 1 2Sample Output1
3
6
9
12
17
A

BZOJ4516: [Sdoi2016] generate spell suffix automaton

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.