题面:http://www.lydsy.com/JudgeOnline/upload/Noi2017D1.pdf

正解:字符串$hash$。

我在考场上写了个$map$的$hash$被卡成$40$分,然后改成挂链版本的就$AC$了。。$mdzz$,以后$hash$再也不写$map$了。。

我们考虑使用链表来表示字符间的关系,合并和分裂都用链表来表示,这样我们可以快速找到两个字符的前$k$个和后$k$个字符。

注意到每次只会增加或减少$k^{2}$个字符串,那么我们直接把这些字符串$hash$起来即可,查询统计也很简单。

这道题好像是被骂得最惨的一道题??有人说出题人想告诉我们$SAM$,后缀数组这些高级字符串数据结构都没用。。

 //It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define rhl (998244353)
#define wyh (19260817)
#define M (10000010)
#define N (300010)
#define il inline
#define RG register
#define ll long long
#define ull unsigned long long using namespace std; struct edge{ int nt,sum; ull v; }g[]; int head[wyh+],a[N],lst[N],nxt[N],st1[],st2[],tong[],n,m,num,fg,top1,top2;
ull hsh[M],bin[M],base=;
char s[M];
ll ans; il int gi(){
RG int x=,q=; RG char ch=getchar();
while ((ch<'' || ch>'') && ch!='-') ch=getchar();
if (ch=='-') q=-,ch=getchar();
while (ch>='' && ch<='') x=x*+ch-,ch=getchar();
return q*x;
} il void insert(RG int from,RG ull to){
g[++num]=(edge){head[from],,to},head[from]=num; return;
} il void add(RG ull Hash){
RG int ymd=Hash%wyh;
for (RG int i=head[ymd];i;i=g[i].nt)
if (g[i].v==Hash){ ++g[i].sum; return; }
return insert(ymd,Hash);
} il void del(RG ull Hash){
RG int ymd=Hash%wyh;
for (RG int i=head[ymd];i;i=g[i].nt)
if (g[i].v==Hash){ --g[i].sum; return; }
return;
} il ll calc(RG ull Hash){
RG int ymd=Hash%wyh;
for (RG int i=head[ymd];i;i=g[i].nt)
if (g[i].v==Hash) return g[i].sum;
return ;
} int main(){
#ifndef ONLINE_JUDGE
freopen("queue.in","r",stdin);
freopen("queue.out","w",stdout);
#endif
n=gi(),m=gi(),fg=,bin[]=;
for (RG int i=;i<=;++i) bin[i]=bin[i-]*base;
for (RG int i=;i<=n;++i){ a[i]=gi(),++tong[a[i]]; if (a[i]!=) fg=; }
for (RG int i=;i<=n;++i) lst[i]=nxt[i]=i;
for (RG int p=,op,x,y,k,len;p<=m;++p){
op=gi();
if (op==){
x=gi(),y=gi(),top1=top2=,st1[++top1]=x,st2[++top2]=y;
for (RG int i=x;top1< && lst[i]!=i;i=lst[i]) st1[++top1]=lst[i];
for (RG int i=y;top2< && nxt[i]!=i;i=nxt[i]) st2[++top2]=nxt[i];
RG ull Hsh=; for (RG int i=top1;i;--i) Hsh=Hsh*base+a[st1[i]]; st1[top1+]=;
for (RG int i=top1;i;--i){
RG ull Hash=Hsh-a[st1[i+]]*bin[i]; Hsh=Hash,len=i;
for (RG int j=;j<=top2;++j){
++len; if (len>) break;
Hash=Hash*base+a[st2[j]],add(Hash);
}
}
nxt[x]=y,lst[y]=x;
}
if (op==){
x=gi(),y=nxt[x],top1=top2=,st1[++top1]=x,st2[++top2]=y;
for (RG int i=x;top1< && lst[i]!=i;i=lst[i]) st1[++top1]=lst[i];
for (RG int i=y;top2< && nxt[i]!=i;i=nxt[i]) st2[++top2]=nxt[i];
RG ull Hsh=; for (RG int i=top1;i;--i) Hsh=Hsh*base+a[st1[i]]; st1[top1+]=;
for (RG int i=top1;i;--i){
RG ull Hash=Hsh-a[st1[i+]]*bin[i]; Hsh=Hash,len=i;
for (RG int j=;j<=top2;++j){
++len; if (len>) break;
Hash=Hash*base+a[st2[j]],del(Hash);
}
}
nxt[x]=x,lst[y]=y;
}
if (op==){
scanf("%s",s+),k=gi(),len=strlen(s+),ans=; RG ull Hash;
if (k==){
for (RG int i=;i<=len;++i) ans=ans*(ll)tong[s[i]-]%rhl;
printf("%lld\n",ans); continue;
}
for (RG int i=;i<=len;++i) hsh[i]=hsh[i-]*base+s[i]-;
for (RG int i=;i+k-<=len;++i){
Hash=hsh[i+k-]-hsh[i-]*bin[k];
ans=ans*calc(Hash)%rhl;
}
printf("%lld\n",ans);
}
}
return ;
}

bzoj4943 [Noi2017]蚯蚓排队的更多相关文章

  1. BZOJ4943 NOI2017蚯蚓排队(哈希+链表)

    能看懂题就能想到正解.维护所有长度不超过k的数字串的哈希值即可,用链表维护一下蚯蚓间连接情况.由于这样的数字串至多只有nk个,计算哈希值的总复杂度为O(nk),而分裂的复杂度为O(ck^2),询问复杂 ...

  2. [Bzoj4943][Noi2017]蚯蚓(hash)

    4943: [Noi2017]蚯蚓 Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 237  Solved: 110[Submit][Status][D ...

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

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

  4. 【uoj#315/bzoj4943】[NOI2017]蚯蚓排队 Hash

    题目描述 给出 $n$ 个字符,初始每个字符单独成字符串.支持 $m$ 次操作,每次为一下三种之一: $1\ i\ j$ :将以 $i$ 结尾的串和以 $j$ 开头的串连到一起. $2\ i$ :将 ...

  5. BZOJ4943 [NOI2017] 蚯蚓

    题目描述 蚯蚓幼儿园有nn 只蚯蚓.幼儿园园长神刀手为了管理方便,时常让这些蚯蚓们列队表演. 所有蚯蚓用从11 到nn 的连续正整数编号.每只蚯蚓的长度可以用一个正整数表示,根据入园要求,所有蚯蚓的长 ...

  6. [NOI2017]蚯蚓排队 hash

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

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

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

  8. P3823_[NOI2017]蚯蚓排队 哈希+脑子

    之前就写过一遍,今天却写挂了,查了半天发现是数组名写错啦$qwq$ 观察到$K$很小,所以使得我们可以哈希(怎么什么都能哈希$qwq$).我们把长度小于等于$50$的子串扔到哈希表里,并统计出现次数, ...

  9. 洛谷P3832 [NOI2017]蚯蚓排队 【链表 + 字符串hash】

    题目链接 洛谷P3832 题解 字符串哈希然后丢到hash表里边查询即可 因为\(k \le 50\),1.2操作就暴力维护一下 经复杂度分析会发现直接这样暴力维护是对的 一开始自然溢出WA了,还以为 ...

随机推荐

  1. my29_PXC集群状态查看

    节点从集群中移除的状态 show status like '%wsrep%';wsrep_cluster_status为Disconnected则表示该节点已经不在集群中了,示例如下 > sho ...

  2. JS 打乱数组顺序

    function rand(arr) { var len = arr.length //首先从最大的数开始遍历,之后递减 for(var i = arr.length - 1; i >= 0; ...

  3. 转 python trace walk DEMO

    https://blog.csdn.net/steadfast123/article/details/46965125 #quote from 'introduction to computation ...

  4. SpringBoot初始教程之Servlet、Filter、Listener配置详解

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  5. STL 部分学习 未整理

    https://www.cnblogs.com/pugang/archive/2012/02/10/2345942.html

  6. 使用cucumber & selenium实现一个简单的bddtest

    1.Cucumber介绍 + feature : read requirement +scenario : testing situation,including + Given/ + when/ + ...

  7. 如何获取Android手机的唯一标识

    有很多场景和需求你需要用到手机设备的唯一标识符. 在Android中,有以下几种方法获取这样的ID. 1. The IMEI: 仅仅只对Android手机有效: 1 2 TelephonyManage ...

  8. Unity Animation.CrossFade Animation.Play

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerCo ...

  9. jemeter、windox下安装教程

    JMeter是Apache软件基金会的产品,用于对静态和动态的资源(文件,Servlet,Perl脚本,Java对象,数据库和查询,FTP服务器等等)的性能进行测试,是一款很方便的测试软件. 系统:w ...

  10. MATLAB字符串处理

    字符串处理 MATLAB中字符串用单引号   如果字符串中存在单引号,要使用两个单引号. length()元素个数 (1)字符串的执行 格式:eval(s) s表示字符串 abs()和double() ...