【BZOJ2081】Beads(哈希表)
2024-08-30 19:56:46
题意:
翻转是指其中一段长度为k的子串全部翻转
n<=200000 a[i]<=n
思路:枚举k,直接哈希判充即可
时间复杂度是n/i求和,根据定理可得是O(n log n)级别的
单哈双哈都可能被卡,我用的是单哈+哈希表判重
const mo=;
var h1,h2,mi,c,a,head,vet,next:array[..]of longint;
n,i,p,j,st,ed,t1,t2,t3,l,ans,tot:longint; function hash1(x,y:longint):longint;
begin
hash1:=(h1[y]-int64(h1[x-])*mi[y-x+] mod mo) mod mo;
hash1:=(hash1+mo) mod mo;
end; function hash2(x,y:longint):longint;
begin
hash2:=(h2[y]-int64(h2[x-])*mi[y-x+] mod mo) mod mo;
hash2:=(hash2+mo) mod mo;
end; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; procedure add(a,b:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
head[a]:=tot;
end; function judge(u,x:longint):boolean;
var e,v:longint;
begin
e:=head[u];
while e<> do
begin
v:=vet[e];
if v=x then exit(false);
e:=next[e];
end;
exit(true);
end; begin
assign(input,'bzoj2081.in'); reset(input);
assign(output,'bzoj2081.out'); rewrite(output);
readln(n);
for i:= to n do read(a[i]);
mi[]:=;
for i:= to n do mi[i]:=int64(mi[i-])* mod mo;
for i:= to n do h1[i]:=(int64(h1[i-])*+a[i]) mod mo;
for i:= to n div do swap(a[i],a[n-i+]);
for i:= to n do h2[i]:=(int64(h2[i-])*+a[i]) mod mo;
for i:= to n do
begin
p:=; tot:=;
for j:= to n div i do
begin
st:=i*(j-)+; ed:=i*j;
t1:=hash1(st,ed); t2:=hash2(n-ed+,n-st+);
t3:=int64(t1)*t2 mod mo;
inc(t3); inc(t1); inc(t2);
if judge(t3,t1) then
begin
inc(p);
add(t3,t1);
add(t3,t2);
end;
end;
if p=ans then begin inc(l); c[l]:=i; end;
if p>ans then
begin
l:=; c[]:=i; ans:=p;
end;
for j:= to n div i do
begin
st:=i*(j-)+; ed:=i*j;
t1:=hash1(st,ed); t2:=hash2(n-ed+,n-st+);
t3:=int64(t1)*t2 mod mo;
inc(t3);
head[t3]:=;
end;
end;
writeln(ans,' ',l);
for i:= to l- do write(c[i],' ');
write(c[l]);
close(input);
close(output);
end.
最新文章
- 回首Java(始)
- ";只能在执行Render()的过程中调用RegisterForEventValidation"; 解决方案
- 【转】shell 教程——02 几种常见的Shell
- 6天通吃树结构—— 第三天 Treap树
- git 简单教程更新
- Jasper_style
- EHCache分布式缓存集群环境配置
- Netty 客户端断线重连
- ios开发中的深拷贝和浅拷贝
- Tensorboard可视化(关于TensorFlow不同版本引起的错误)
- flask 搭建ssl接口
- mysql 数据库查看表的信息
- AI之旅(5):正则化与牛顿方法
- Selenium+TestNG+Maven+Jenkins+SVN(转载)
- javascript的Mixins
- Windos 下python2.7安装 pymssql 解决方案
- [php] try - catch exceptiong handler
- ACTGame项目
- vmware下载存储vmdk文件后缀变-flat处理方式
- BZOJ4921 互质序列