★★☆ input file: menci_incantation.in output file: menci_incantation.out simple comparison
Time limit: 1 s memory limit: MB
"Title description"
The 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 Format"
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.
"Output Format"
Output n rows, one number per line. The number of line I represents the number of enchantments generated by S after the first operation.
"Sample Input"
7
1 2 3 3 3 1 2
"Sample Output"
1
3
6
9
12
17
22
Prompted
For 10% of data, 1≤n≤10.
For 30% of data, 1≤n≤100.
For 60% of data, 1≤n≤1000.
For 100% of data, 1≤n≤100000.
The number x that is used to denote the spell character satisfies the 1≤x≤10^9.
Exercises
Test instructions is to add a character after a string, and ask how much of the string is now different after adding the character.
Ask for the number of different substrings, think of a classic application of the suffix array, that is, by height[] to find how many strings in a string are essentially different substrings. For this problem, we can think that each substring is a suffix prefix, then the problem is converted to each suffix, to find out what it can contribute to the number of different prefixes, the answer is cumulative. As a few examples, the answer to each suffix contribution is the length of the suffix minus the maximum value of the previous suffix and the LCP, which is the maximum value of height[i].
For this problem, because the string is by a null character one to add and one to ask, so you can first flip the string into the form of a suffix array, the answer is to ask the length of the current string minus the existing string and its LCP maximum value. The key is how to ask for this LCP, this value by the description of the second paragraph can be seen, assuming that the current string of rank=k, and it constitutes the maximum value of LCP string rank should be k+1 or K-1. This can be used hash+ +set maintenance, set to find precursors and successors, two to compare the size of two strings, the principle is two separate two strings of LCP, judging the next bit size can be.
1#include <bits/stdc++.h>2 using namespacestd;3typedef unsignedLong LongULL;4 Const intmaxn=200000;5 ConstULL base=1e9+7;6 intN,now;7ULL A[MAXN],Base[Maxn],hash[maxn],ans;8Inline ULL Get_hash (ULL from, ULL Len) {9 returnhash[ from]-hash[ from+len]*Base[Len];Ten } OneInlineintFindintLintRintF1,intF2) { A if(L +1>=R) { - if(Get_hash (F1,r) ==get_hash (f2,r))returnR; - Else returnl; the } - intMid= (l+r) >>1; - if(Get_hash (F1,mid) ==get_hash (F2,mid))returnfind (MID,R,F1,F2); - Else returnFind (l,mid-1, F1,F2); + } - structcmp{ + BOOL operator()(Const int&AA,Const int&b) { A intLen=find (0, 1E5,AA,BB); at returna[aa+len]<a[bb+Len]; - } - }; - Set<int,cmp>S; - Set<int,cmp>:: iterator tmp1,tmp2; - intMain () { inscanf"%d",&N); - Base[0]=1; for(intI=1; i<=n;i++)Base[i]=Base[I1]*BASE; to for(inti=n;i>=1; i--){ +scanf"%d", &a[i]); now=0; -hash[i]=hash[i+1]*base+A[i]; thetmp1=S.insert (i). First; *Tmp2=Tmp1; $ if(tmp1!=S.begin ()) {Panax Notoginsengtmp1--; -Now=find (0, 1e5,i,*tmp1); the } + if(++tmp2!=S.end ()) { ANow=max (Now,find (0, 1e5,i,*tmp2)); the } +ans+= (ULL) n-i+1-Now ; -printf"%llu\n", ANS); $ } $ return 0; -}
Cogs 2223. [SDOI2016 Round1] Generate enchantments