题目链接

洛谷P3832

题解

字符串哈希然后丢到hash表里边查询即可

因为\(k \le 50\),1、2操作就暴力维护一下

经复杂度分析会发现直接这样暴力维护是对的

一开始自然溢出WA了,还以为是哈希冲突,改成双哈希后依旧WA

后来才发现是sb了漏了一句QAQ

不卡自然溢出

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long int
#define ULL unsigned long long int
#define REP(i,n) for (register int i = 1; i <= (n); i++)
#define res register
using namespace std;
const int maxn = 200005,maxh = 10000005,INF = 1000000000;
const int md = 998244353,P = 19260817;
inline int read(){
res int out = 0,flag = 1; res char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
inline void write(int x){
if (x / 10) write(x / 10);
putchar(x % 10 + '0');
}
struct Hash_Chart{
ULL num[maxh];
int head[P + 5],nxt[maxh],cnt,sum[maxh];
void ins(ULL x){
int id = x % P;
if (!head[id]){
head[id] = ++cnt;
num[cnt] = x;
sum[cnt] = 1;
return;
}
for (res int k = head[id]; k; k = nxt[k]){
if (num[k] == x) {sum[k]++; return;}
if (!nxt[k]){
nxt[k] = ++cnt;
num[cnt] = x;
sum[cnt] = 1;
return;
}
}
}
void del(ULL x){
int id = x % P;
for (res int k = head[id]; k; k = nxt[k])
if (num[k] == x) {sum[k]--; return;}
}
int find(ULL x){
int id = x % P;
for (res int k = head[id]; k; k = nxt[k]){
if (num[k] == x) return sum[k];
}
return 0;
}
}hash;
ULL bin[maxn],num[maxn],s1[maxn],s2[maxn];
int lk[maxn],rk[maxn],t1,t2,n,m;
char s[maxh];
inline void solve1(){
int u = read(),v = read();
t1 = 0;
for (res int k = u; k && t1 < 50; k = lk[k]){
++t1;
s1[t1] = s1[t1 - 1] + bin[t1 - 1] * num[k];
}
t2 = 0;
for (res int k = v; k && t2 < 50; k = rk[k]){
++t2;
s2[t2] = s2[t2 - 1] * 7 + num[k];
}
for (res int i = 1; i <= t1; i++)
for (res int j = 1; j <= t2 && i + j <= 50; j++)
hash.ins(s2[j] + s1[i] * bin[j]);
rk[u] = v; lk[v] = u;
}
inline void solve2(){
int u = read(),v = rk[u];
t1 = 0;
for (res int k = u; k && t1 < 50; k = lk[k]){
++t1;
s1[t1] = s1[t1 - 1] + bin[t1 - 1] * num[k];
}
t2 = 0;
for (res int k = v; k && t2 < 50; k = rk[k]){
++t2;
s2[t2] = s2[t2 - 1] * 7 + num[k];
}
for (res int i = 1; i <= t1; i++)
for (res int j = 1; j <= t2 && i + j <= 50; j++)
hash.del(s2[j] + s1[i] * bin[j]);
rk[u] = lk[v] = 0;
}
inline void solve3(){
scanf("%s",s + 1); s[0] = '0';
int len = strlen(s + 1),k = read();
LL ans = 1; ULL h = 0;
for (res int i = 1; i <= len; i++){
h = h * 7 + s[i] - '0';
if (i >= k){
h -= bin[k] * (s[i - k] - '0');
ans = ans * hash.find(h) % md;
}
}
write(ans); putchar('\n');
}
int main(){
bin[0] = 1; for (res int i = 1; i < maxn; i++) bin[i] = bin[i - 1] * 7;
n = read(); m = read(); int opt;
REP(i,n) hash.ins(num[i] = read());
while (m--){
opt = read();
if (opt == 1) solve1();
else if (opt == 2) solve2();
else solve3();
}
return 0;
}

洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】的更多相关文章

  1. 洛谷3823 [NOI2017] 蚯蚓排队 【哈希】

    题目分析: 从$\sum|S|$入手.共考虑$\sum|S|$个$f(t)$.所以我们要一个对于每个$f(t)$在$O(1)$求解的算法.不难想到是哈希. 然后考虑分裂和合并操作.一次合并操作要考虑合 ...

  2. 洛谷 P4036 [JSOI2008]火星人(splay+字符串hash)

    题面 洛谷 题解 首先,我们知道求最长公共前缀可以用二分答案+hash来求 因为有修改操作, 考虑将整个字符串的hash值放入splay中 接着就是splay的基本操作了 Code #include& ...

  3. 洛谷P3234 抄卡组 [HNOI2014] 字符串hash

    正解:字符串hash 解题报告: 传送门! 字符串hash是字符串匹配中很常见的一个方法,原理也很好懂,这里就不做太多阐述辣有时间放到hash笔记里面去QAQ 题意不说了挺好理解的,自带一句话概括好评 ...

  4. 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]

    题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...

  5. 【BZOJ】4721: [Noip2016]蚯蚓 / 【洛谷】P2827 蚯蚓(单调队列)

    Description 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」=[3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳 蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮 ...

  6. BZOJ4943 & 洛谷3823 & UOJ315:[NOI2017]蚯蚓排队——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4943 http://uoj.ac/problem/315 https://www.luogu.or ...

  7. [NOI2017]蚯蚓排队 hash

    题面:洛谷 题解: 我们暴力维护当前所有队伍内的所有子串(长度k = 1 ~ 50)的出现次数. 把每个子串都用一个hash值来表示,每次改变队伍形态都用双向链表维护,并暴力更新出现次数. 现在考虑复 ...

  8. 【题解】洛谷P2827 [NOIP2016TG] 蚯蚓(优先队列)

    题目来源:洛谷P2827 思路 阅读理解题 一开始以为是裸的优先队列而已 但是发现维护一个切开并且其他的要分别加上一个值很不方便 而且如果直接用优先队列会TLE3到4个点 自测85分 所以我们需要发现 ...

  9. 洛谷3825 [NOI2017]游戏 2-sat

    原文链接http://www.cnblogs.com/zhouzhendong/p/8146041.html 题目传送门 - 洛谷3825 题解 我们考虑到地图中x的个数很少,最多只有8个. 所以我们 ...

随机推荐

  1. 在RichTextBox控件中替换文本文字

    实现效果: 知识运用: RichTextBox控件的SelectedText属性 实现代码: private void button1_Click(object sender, EventArgs e ...

  2. appium---adb通过wifi连接手机

    前几天接到领导的安排,想要测试下apk的耗电量,可以通过手机adb命令进行监控手机电量的变化:但是这样如果通过USB连接手机的话,USB就会自动给手机进行充电,无法达到我们想要的结果,于是想到了通过w ...

  3. AngularJs学习笔记-数据绑定、管道

    数据绑定.管道 (1)数据绑定(Angular中默认是单向绑定) 1.[]方括号 可以用于子组件传值 由于是单向绑定,所以当子组件中的iStars属性发生改变时,不会影响到父组件中product.ra ...

  4. for循环语句中的先后执行顺序

    for(int i=0;i<10;i++){ cout<<i; } 分析程序运行结果:for(cout<<"a";cout<<" ...

  5. RSA等非对称加密为什么要用公钥加密,而用私钥解密?

    1.RSA是不对称加密算法,它的公钥可能会被多人持有(公钥公钥,公开的密钥),而私钥只有一人拥有,例如支付宝开放平台,私钥只有支付宝公司持有,而公钥则是所有接入它API的公司都能得到.对于公钥加密的信 ...

  6. vitrual box安装centos时一直黑屏的解决办法

    趁着清明节没事,昨天看了mysql性能优化后,想装个linux系统学习下,linux一直是我的短板...之前是在vmware上安装ubuntu,买了新电脑后,听过virtual box相比vmware ...

  7. jsp页面:一个form,不同请求提交form

    需求:一个表单中有一个请求 action="url"发送数据地址: 在表单外有一个请求,请求form表单提交的数据 我们用js来写:通过每次请求传不同的action=url; 例如 ...

  8. <Docker学习>1. 简介

    Q: Dokcer是什么? A: 是一种虚拟化技术.参考https://www.imooc.com/learn/867快速了解Docker. Q: 传统虚拟机技术和Dokcer的区别? A: 传统虚拟 ...

  9. ICSharpCode.SharpZipLib.dll

    using ICSharpCode.SharpZipLib.Checksums; using ICSharpCode.SharpZipLib.Zip; namespace { /// <summ ...

  10. Bubblesort冒泡算法

    最简单的算法,大家都知道两层for循环,中间加一个过渡用来交换数据 小例子: package com.neuedu.algorithm;//算法 public class Bubblesort { / ...