4516: [Sdoi2016]生成魔咒
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1026 Solved: 576
[ 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
1 2 3 3 3 1 2
Sample Output
1
3
6
9
12
17
22
3
6
9
12
17
22
HINT
Source
【分析】
貌似和hdu 4622是一样的...?
但是字符太多,要用map。
C++大法好,map把你保(除了坑爹JLOI还卡map)
【代码】
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=100005;
ll ans;
map <int,int> son[mxn<<1];
int n,len,tot,root,p,q,np,nq;
int s[mxn],pre[mxn<<1],step[mxn<<1];
inline void sam()
{
int i,j;
root=tot=np=1;
fo(i,1,len)
{
int c=s[i];
p=np;
step[np=(++tot)]=step[p]+1;
while(p && !son[p].count(c))
son[p][c]=np,p=pre[p];
if(!p)
{
pre[np]=root;
ans+=step[np];
printf("%lld\n",ans);
continue;
}
q=son[p][c];
if(step[p]+1==step[q])
pre[np]=q;
else
{
step[nq=(++tot)]=step[p]+1;
son[nq]=son[q];
pre[nq]=pre[q];
pre[q]=pre[np]=nq;
while(p && son[p][c]==q)
son[p][c]=nq,p=pre[p];
}
ans+=step[np]-step[pre[np]];
printf("%lld\n",ans);
}
}
int main()
{
int i,j;
scanf("%d",&len);
fo(i,1,len) scanf("%d",&s[i]);
sam();
return 0;
}