传送门

samsamsam入门题。

题意简述:给出一个串让你依次插入字符,求每次插入字符之后不同子串的数量。


显然每次的变化量只跟新出现的nnn个后缀有关系,那么显然就是maxlenp−maxlenlinkpmaxlen_p-maxlen_{link_p}maxlenp​−maxlenlinkp​​。

注意到字符集很大,可以用mapmapmap来维护转移边。

代码:

#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
	int ans=0;
	char ch=getchar();
	while(!isdigit(ch))ch=getchar();
	while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
	return ans;
}
const int N=2e5+5;
int n;
typedef long long ll;
ll ans=0;
struct SAM{
	int tot,last,len[N],link[N];
	map<int,int>son[N];
	SAM(){last=tot=1,len[0]=-1;}
	inline void expand(int x){
		int p=last,np=++tot;
		len[last=np]=len[p]+1;
		while(p&&!son[p][x])son[p][x]=np,p=link[p];
		if(!p)link[np]=1;
		else{
			int q=son[p][x],nq;
			if(len[q]==len[p]+1)link[np]=q;
			else{
				len[nq=++tot]=len[p]+1,son[nq]=son[q],link[nq]=link[q],link[q]=link[np]=nq;
				while(p&&son[p][x]==q)son[p][x]=nq,p=link[p];
			}
		}
		ans+=(ll)len[np]-len[link[np]];
	}
}sam;
int main(){
	n=read();
	for(ri i=1;i<=n;++i)sam.expand(read()),cout<<ans<<'\n';
	return 0;
}

2018.12.23 bzoj4516: [Sdoi2016]生成魔咒(后缀自动机)的更多相关文章

  1. BZOJ4516: [Sdoi2016]生成魔咒 后缀自动机

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> #inclu ...

  2. [bzoj4516][Sdoi2016]生成魔咒——后缀自动机

    Brief Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2]. 一个魔咒串 S 的非空字串被称为魔咒串 S 的生 ...

  3. BZOJ 4516: [Sdoi2016]生成魔咒 [后缀自动机]

    4516: [Sdoi2016]生成魔咒 题意:询问一个字符串每个前缀有多少不同的子串 做了一下SDOI2016R1D2,题好水啊随便AK 强行开map上SAM 每个状态的贡献就是\(Max(s)-M ...

  4. BZOJ4516: [Sdoi2016]生成魔咒(后缀数组 set RMQ)

    题意 题目链接 Sol 毒瘤SDOI 终于有一道我会做的题啦qwq 首先,本质不同的子串的个数 $ = \frac{n(n + 1)}{2} - \sum height[i]$ 把原串翻转过来,每次就 ...

  5. [SDOI2016]生成魔咒(后缀自动机)

    /* 水题, 根据性质做就行, nq不会对答案产生贡献, 那么只算p的贡献就好了 */ #include<cstdio> #include<algorithm> #includ ...

  6. BZOJ.4516.[SDOI2016]生成魔咒(后缀自动机 map)

    题目链接 后缀数组做法见这. 直接SAM+map.对于每个节点其产生的不同子串数为len[i]-len[fa[i]]. //15932kb 676ms #include <map> #in ...

  7. BZOJ 4516: [Sdoi2016]生成魔咒 后缀自动机 性质

    http://www.lydsy.com/JudgeOnline/problem.php?id=4516 http://blog.csdn.net/doyouseeman/article/detail ...

  8. BZOJ 4516 [Sdoi2016]生成魔咒 ——后缀自动机

    本质不同的字串,考虑SA的做法,比较弱,貌似不会. 好吧,只好用SAM了,由于后缀自动机的状态最简的性质, 所有不同的字串就是∑l[i]-l[fa[i]], 然后后缀自动机是可以在线的,然后维护一下就 ...

  9. [SDOI2016] 生成魔咒 - 后缀数组,平衡树,STL,时间倒流

    [SDOI2016] 生成魔咒 Description 初态串为空,每次在末尾追加一个字符,动态维护本质不同的子串数. Solution 考虑时间倒流,并将串反转,则变为每次从开头删掉一个字符,即每次 ...

随机推荐

  1. PAT1026 (大模拟)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  2. 高级测试岗位面试题---MARK

    直接手写一个python类 直接手写一个构造函数 紧接着上面的代码,直接手写,补充完整代码,要求对列表中的人进行排序,并筛选出分数大于80的人的名单,组成一个新的列表显示出来. class Perso ...

  3. 188. Best Time to Buy and Sell Stock IV (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. <asp:Button点击查询后,调用js中函数展现加载圈

    <div> <div id='paneloading' style='display:none;position:fixed;top:0px;left:0px;z-index:999 ...

  5. unity农场模拟经营游戏源码

    下载地址: https://item.taobao.com/item.htm?spm=a1z10.5-c-s.w4002-12305352547.10.25ca3c4eWAibvf&id=56 ...

  6. SDK和API

    软件开发工具包(缩写:SDK.外语全称:Software Development Kit)一般都是一些软件工程师为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件时的开发工具的集合. 笔记:开 ...

  7. [leetcode]252. Meeting Rooms会议室有冲突吗

    Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...

  8. mysql left join 多条记录 1:n 的处理方法

    一.准备两张表,文章表和评伦表 CREATE TABLE `article` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', ...

  9. IE7下面踩得坑

    bug1.position:fixed:z-index:99; 出现了z-index:2的层级跑到他上面了, 为什么?会出现这问题??? 检查: 1你的固定定位的容器是不是被其他容器包裹,你包裹得容器 ...

  10. Django 导入css文件,样式不起作用。Resource interpreted as Stylesheet but transferred with MIME type application/x-css

    笔者今天在模板中加载css文件时,发现 css样式能够下载再来却无法起作用,而且,图片.js都能够正常使用. 并且 浏览器提示: Resource interpreted as Stylesheet ...