【LG4070】[SDOI2016]生成魔咒

题面

洛谷

题解

如果我们不用在线输的话,那么答案就是对于所有状态\(i\)

\[\sum (i.len-i.fa.len)
\]

现在我们需要在线询问,那么因为\(SAM\)是在线算法,我们考虑每次的对答案的贡献。

那么产生的贡献就是\(last.len-last.fa.len\)。

与\(yyb\)的对话:

Q:为什么构建自动机时中间过程新加的点不会算到最后答案中呢?

A:不影响答案啊,你在两个len之间断开,对于答案的贡献不变。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <tr1/unordered_map>
using namespace std;
using namespace std::tr1;
inline int gi() {
register int data = 0, w = 1;
register char ch = 0;
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') w = -1, ch = getchar();
while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
return w * data;
}
const int MAX_N = 1e5 + 5;
struct Node {
unordered_map<int, int> ch;
int len, fa;
} t[MAX_N << 1];
int tot = 1, lst = 1;
long long ans = 0;
void extend(int c) {
int New = ++tot;
t[lst].ch[c] = New;
t[New].len = t[lst].len + 1;
int p = t[lst].fa; lst = tot;
while (p && t[p].ch.find(c) == t[p].ch.end()) t[p].ch[c] = New, p = t[p].fa;
if (!p) t[New].fa = 1;
else {
int q = t[p].ch[c];
if (t[q].len == t[p].len + 1) t[New].fa = q;
else {
int _q = ++tot; t[_q] = t[q];
t[New].fa = t[q].fa = _q, t[_q].len = t[p].len + 1;
while (p) {
unordered_map<int, int> :: iterator ite;
ite = t[p].ch.find(c);
if (ite == t[p].ch.end() || ite->second != q) break;
t[p].ch[c] = _q;
p = t[p].fa;
}
}
}
ans += t[New].len - t[t[New].fa].len;
} int main () {
#ifndef ONLINE_JUDGE
freopen("cpp.in", "r", stdin);
#endif
int N = gi();
for (int i = 1; i <= N; i++) extend(gi()), printf("%lld\n", ans);
return 0;
}

【LG4070】[SDOI2016]生成魔咒的更多相关文章

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

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

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

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

  3. BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay

    BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...

  4. P4070 [SDOI2016]生成魔咒

    题目地址:P4070 [SDOI2016]生成魔咒 相信看到题目之后很多人跟我的思路是一样的-- 肯定要用 SA(P3809 [模板]后缀排序) 肯定要会求本质不同的子串个数(P2408 不同子串个数 ...

  5. bzoj4516 / P4070 [SDOI2016]生成魔咒

    P4070 [SDOI2016]生成魔咒 后缀自动机 每插入一个字符,对答案的贡献为$len[last]-len[fa[last]]$ 插入字符范围过大,所以使用$map$存储. (去掉第35行就是裸 ...

  6. 洛谷 P4070 [SDOI2016]生成魔咒 解题报告

    P4070 [SDOI2016]生成魔咒 题目描述 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 \(1\).\(2\) 拼凑起来形成一个魔咒串 \([1,2]\). 一个魔咒 ...

  7. [Sdoi2016]生成魔咒[SAM or SA]

    4516: [Sdoi2016]生成魔咒 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1017  Solved: 569[Submit][Statu ...

  8. 4516: [Sdoi2016]生成魔咒

    4516: [Sdoi2016]生成魔咒 链接 题意: 求本质不同的子串. 分析: 后缀数组或者SAM都可以. 考虑SAM中每个点的可以表示的子串是一个区间min(S)~max(S),把每个点的这个区 ...

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

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

随机推荐

  1. LeetCode题解之N-ary Tree Postorder Traversal

    1.题目描述 2.问题分析 递归. 3.代码 vector<int> postorder(Node* root) { vector<int> v; postNorder(roo ...

  2. SQL Server 合并复制的Article可以指定单个对象的更新方向

    如下所示,这是SQL Server中一个合并复制发布端的Article: 我们可以在Article中选择一个对象,比如这里我们选择MD.Car表,点击鼠标右键,选择"Set Properti ...

  3. sqlserver性能调优中的逻辑读,物理读,预读是什么意思

    表 'T_EPZ_INOUT_ENTRY_DETAIL'.扫描计数 1,逻辑读 4825 次,物理读 6 次,预读 19672 次.SQL SERVER 数据库引擎当遇到一个查询语句时,SQL SER ...

  4. 【优质】React的学习资源

    React的学习资源 github 地址: https://github.com/LeuisKen/react-collection https://github.com/reactnativecn/ ...

  5. codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem

    dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.

  6. Python接口自动化--requests 2

    # _*_ encoding:utf-8 _*_ import json import requests #post请求 payload = {"cindy":"hell ...

  7. ap、map值计算

    ap:所有图片某一个类 map:所有图片所有类的ap的平均 以一个score为阈值,大于score的所有框是假定正确输出的所有预测,将这些框和gt匹配(iou大于某一个阈值认为匹配成功),得到当前sc ...

  8. JSP中使用JDBC连接MySQL数据库的详细步骤

    1,首先在自己新建的项目文本框中输入Web Project的名称,然后单击下一步. 2,继续单击下一步 3,把Generate web.xml deployment descriptor复选框勾上. ...

  9. WorldWind源码剖析系列:网络下载类WebDownload

    网络下载类WebDownload封装了对请求的瓦片进行网络下载的相关操作.该类使用了两个委托类型和一个枚举类型. 该类的类图如下. 网络下载类WebDownload各个字段和属性的含义说明如下: st ...

  10. leetcode70—Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...