[NOI2017]蚯蚓排队
嘟嘟嘟
现在看来这道题还不是特别难。
别一看到字符串就想SAM
看到\(k\)很小,所以我们可以搞一个单次修改复杂度跟\(k\)有关的算法。
能想到,每一次断开或链接,最多只会影响\(k ^ 2\)个长度为\(k\)的区间。所以我们开一个哈希表,每一次拼接时就往哈希表里加入\(k ^ 2\)个新的哈希值,断链的时候就把这些哈希值减去。然后查询的时候只要扫一遍\(s\),每遇到长度为\(k\)的子串就再查一下。
具体的操作要用到链表,对于每一个节点分别维护一个pre和suf指针即可。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const ll mod = 998244353;
const int maxn = 2e5 + 5;
const int maxm = 2e7 + 5;
const int LIM = 50;
const ull NUM = 233;
const ull P = 19260817;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
freopen("worm.in", "r", stdin);
freopen("worm.out", "w", stdout);
#endif
}
char s[maxm];
int n, m, a[maxn];
struct Hash
{
int head[maxm], nxt[maxm], val[maxm], tot;
ull to[maxm];
In void update(ull x, int sum)
{
int h = x % P;
for(int i = head[h]; i; i = nxt[i])
if(to[i] == x) {val[i] += sum; return;}
nxt[++tot] = head[h], to[tot] = x, val[tot] = sum, head[h] = tot;
}
In int query(ull x)
{
for(int i = head[x % P]; i; i = nxt[i])
if(to[i] == x) return val[i];
return 0;
}
}H;
ull p[maxn];
int pre[maxn], suf[maxn];
In void merge(int x, int y)
{
ull now = 0;
pre[y] = x, suf[x] = y;
for(int i = x, l1 = 1; i && l1 <= LIM; i = pre[i], ++l1)
{
now += p[l1 - 1] * a[i]; ull tp = now;
for(int j = y, l2 = l1 + 1; j && l2 <= LIM; j = suf[j], ++l2)
{
tp = tp * NUM + a[j];
H.update(tp, 1);
}
}
}
In void split(int x)
{
int y = suf[x]; ull now = 0;
for(int i = x, l1 = 1; i && l1 <= LIM; i = pre[i], ++l1)
{
now += p[l1 - 1] * a[i]; ull tp = now;
for(int j = y, l2 = l1 + 1; j && l2 <= LIM; j = suf[j], ++l2)
{
tp = tp * NUM + a[j];
H.update(tp, -1);
}
}
suf[x] = 0, pre[y] = 0;
}
In ll query(const int& k)
{
ull now = 0, ret = 1;
int len = strlen(s);
for(int i = 0; i < len; ++i) //滚动哈希
{
now = now * NUM + (s[i] - '0');
if(i >= k - 1)
{
ret = ret * H.query(now) % mod;
now -= p[k - 1] * (s[i - k + 1] - '0');
}
}
return ret;
}
int main()
{
MYFILE();
n = read(), m = read();
for(int i = 1; i <= n; ++i) a[i] = read(), H.update(a[i], 1);
p[0] = 1;
for(int i = 1; i <= LIM; ++i) p[i] = p[i - 1] * NUM;
for(int i = 1; i <= m; ++i)
{
int op = read();
if(op == 1)
{
int x = read(), y = read();
merge(x, y);
}
else if(op == 2)
{
int x = read();
split(x);
}
else
{
scanf("%s", s); int k = read();
write(query(k)), enter;
}
}
return 0;
}
[NOI2017]蚯蚓排队的更多相关文章
- BZOJ4943 NOI2017蚯蚓排队(哈希+链表)
能看懂题就能想到正解.维护所有长度不超过k的数字串的哈希值即可,用链表维护一下蚯蚓间连接情况.由于这样的数字串至多只有nk个,计算哈希值的总复杂度为O(nk),而分裂的复杂度为O(ck^2),询问复杂 ...
- [NOI2017]蚯蚓排队 hash
题面:洛谷 题解: 我们暴力维护当前所有队伍内的所有子串(长度k = 1 ~ 50)的出现次数. 把每个子串都用一个hash值来表示,每次改变队伍形态都用双向链表维护,并暴力更新出现次数. 现在考虑复 ...
- BZOJ4943 & 洛谷3823 & UOJ315:[NOI2017]蚯蚓排队——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=4943 http://uoj.ac/problem/315 https://www.luogu.or ...
- 洛谷3823 [NOI2017] 蚯蚓排队 【哈希】
题目分析: 从$\sum|S|$入手.共考虑$\sum|S|$个$f(t)$.所以我们要一个对于每个$f(t)$在$O(1)$求解的算法.不难想到是哈希. 然后考虑分裂和合并操作.一次合并操作要考虑合 ...
- 【uoj#315/bzoj4943】[NOI2017]蚯蚓排队 Hash
题目描述 给出 $n$ 个字符,初始每个字符单独成字符串.支持 $m$ 次操作,每次为一下三种之一: $1\ i\ j$ :将以 $i$ 结尾的串和以 $j$ 开头的串连到一起. $2\ i$ :将 ...
- bzoj4943 [Noi2017]蚯蚓排队
题面:http://www.lydsy.com/JudgeOnline/upload/Noi2017D1.pdf 正解:字符串$hash$. 我在考场上写了个$map$的$hash$被卡成$40$分, ...
- P3823_[NOI2017]蚯蚓排队 哈希+脑子
之前就写过一遍,今天却写挂了,查了半天发现是数组名写错啦$qwq$ 观察到$K$很小,所以使得我们可以哈希(怎么什么都能哈希$qwq$).我们把长度小于等于$50$的子串扔到哈希表里,并统计出现次数, ...
- 洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】
题目链接 洛谷P3832 题解 字符串哈希然后丢到hash表里边查询即可 因为\(k \le 50\),1.2操作就暴力维护一下 经复杂度分析会发现直接这样暴力维护是对的 一开始自然溢出WA了,还以为 ...
- [NOI2017]蚯蚓排队(链表+hash)
这题看题面感觉挺玄学的,但其实会挂链式hash就能暴力切了,就是纸老虎,考察选手的语文水平.不过三年没写挂链hash也应该写一下了…… 首先模数设成自然溢出ull,然后挂链时的模数取2^24.然后就可 ...
随机推荐
- 【Python基础】09_Python中的元组
1.元组的定义 Tuple (元组)与列表类似,元组的元素 不能修改 元组通常保存 不同类型 的数据 元组用()定义 info_tuple = ("张三", 18, 1.75) 定 ...
- 代理模式与动态代理之JDK实现和CGlib实现
静态代理 静态代理中的代理类和委托类会实现同一接口或是派生自相同的父类. 由业务实现类.业务代理类 两部分组成.业务实现类 负责实现主要的业务方法,业务代理类负责对调用的业务方法作拦截.过滤.预处理, ...
- php对象转换为数组的部分代码
function object_array($array){ if(is_object($array)){ $array = (array)$array; } if(is_array($array)) ...
- .net Core 图片验证码 基于SkiaSharp实现
public class ImageCaptcha { /// <summary> /// 干扰线的颜色集合 /// </summary> private List<SK ...
- netstat用法详解
netstat用法详解 知识,netstat用法详解 图片 netstat用法详解 内容,netstat用法详介绍,netstat用法详正文 netstat命令是一个监控TCP/IP网络的非常有用的工 ...
- 转载一篇让你全面了解什么是.NET。
转载于:https://www.52abp.com/BlogDetails/10009 阅读文本大概需要 8 分钟. 持续进化的 .NET 这张图即是一个学习的路线图同样他也是 .NET 平台的进化图 ...
- About Spring MVC
一.简介 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想,将web层进行职责解 ...
- 1.JavaWeb 知识点概览
1.tomcat服务器的安装和配置.http协议 1.1 虚拟目录的 /*映射*/(配置Context元素)(server.xml catalina\localhost\) http://blog.c ...
- 随意软连接/home/users目录导致环境变量消失后的事故
1 自己的用户zj下,把/home/zj 删除后用ln -s软连接其他目录,导致了当前用户的.bash_profile失效 2 解决思路 第一,删除软连接 rm -rf /home/zj 记住后面 ...
- web开发:清浮动
一.display总结 二.overflow 三.浮动布局 四.清浮动 五.清浮动的方式 一.display总结 <!DOCTYPE html> <html> <head ...