bzoj2351 2462

我没写hash,写了一些奇怪的做法,好像被hash随便操了……

如果没有多测,那么这道题是白书上的例题

把询问矩阵当作a个模板串,建成一个ac自动机

把一开始的矩阵当作n个串放到自动机上匹配,找到a个模板串所有出现的位置

然后找到对应矩阵的左上角上计数(即如果是第i个模板串出现在第x个串的第y个位置,则g[x-i+1,y-b+1]++)

然后判断是否有能接收a个模板串的左上角即可

加了一些剪枝过了2462……

然后2351无限TLE……

要来数据发现,当a=4 b=5的那个点跑了好长时间

有什么做法可以在a非常小的时候快速出解呢?(我就是不想写hash……)

由于询问的a,b都是一定的,我们可以把a行的矩阵压成一个串,每一列压成一个二进制位即可

这样就是给定n-a+1个长度为m的串,问一个长度为b的串是否是其中一个串的子串

这……后缀自动机随便做,这个点就跑得飞快了(因为这样复杂度是O(2^a*n*m+q*a*b了)

注意一下内存……

附上猥琐的代码

  1 type node=record
  2        po,next:longint;
  3      end;
  4
  5 var trie,po:array[0..50010,‘0‘..‘1‘] of longint;
  6     go:array[0..1500010,0..15] of longint;
  7     w,f:array[0..1500010] of longint;
  8     p,q:array[0..50010] of longint;
  9     g,v:array[0..1010,0..1010] of longint;
 10     e:array[0..1010] of node;
 11     te,last,n,m,a,b,len,ans,i,j,t,tot,k,l:longint;
 12     c:array[0..1010] of ansistring;
 13     s:ansistring;
 14     ch:char;
 15
 16 procedure add(x,y:longint);
 17   begin
 18     inc(len);
 19     e[len].po:=y;
 20     e[len].next:=p[x];
 21     p[x]:=len;
 22   end;
 23
 24 procedure fill(x0,y0,x:longint);
 25   var i,y,xx,yy:longint;
 26   begin
 27     i:=p[x];
 28     while i<>0 do
 29     begin
 30       y:=e[i].po;
 31       xx:=x0-y+1;
 32       yy:=y0-b+1;
 33       if xx>0 then
 34       begin
 35         if v[xx,yy]<>te then
 36         begin
 37           v[xx,yy]:=te;
 38           g[xx,yy]:=0;
 39         end;
 40         inc(g[xx,yy]);
 41         if g[xx,yy]>ans then ans:=g[xx,yy];
 42         if ans=a then exit;
 43       end;
 44       i:=e[i].next;
 45     end;
 46   end;
 47
 48 procedure ac;
 49   var j,h,r,x,y:longint;
 50       c:char;
 51   begin
 52     h:=1;
 53     r:=0;
 54     for c:=‘0‘ to ‘1‘ do
 55       if trie[0,c]>0 then
 56       begin
 57         inc(r);
 58         q[r]:=trie[0,c];
 59         f[trie[0,c]]:=0;
 60       end;
 61
 62     while h<=r do
 63     begin
 64       x:=q[h];
 65       for c:=‘0‘ to ‘1‘ do
 66         if trie[x,c]>0 then
 67         begin
 68           y:=trie[x,c];
 69           inc(r);
 70           q[r]:=y;
 71           j:=f[x];
 72           while (j>0) and (trie[j,c]=0) do j:=f[j];
 73           f[y]:=trie[j,c];
 74         end;
 75       inc(h);
 76     end;
 77   end;
 78
 79 procedure change(c:longint);
 80   var q,p,np:longint;
 81   begin
 82     p:=go[last,c];
 83     if w[p]=w[last]+1 then last:=p
 84     else begin
 85       inc(t); np:=t;
 86       w[np]:=w[last]+1;
 87       go[np]:=go[p];
 88       f[np]:=f[p];
 89       f[p]:=np;
 90       q:=last;
 91       while go[q,c]=p do
 92       begin
 93         go[q,c]:=np;
 94         q:=f[q];
 95       end;
 96       last:=np;
 97     end;
 98   end;
 99
100 procedure ins(c:longint);
101   var np,nq,p,q:longint;
102   begin
103     p:=last;
104     inc(t); last:=t; np:=t;
105     w[np]:=w[p]+1;
106     while (p<>0) and (go[p,c]=0) do
107     begin
108       go[p,c]:=np;
109       p:=f[p];
110     end;
111     if p=0 then f[np]:=1
112     else begin
113       q:=go[p,c];
114       if w[q]=w[p]+1 then f[np]:=q
115       else begin
116         inc(t); nq:=t;
117         w[nq]:=w[p]+1;
118         go[nq]:=go[q];
119         f[nq]:=f[q];
120         f[q]:=nq; f[np]:=nq;
121         while go[p,c]=q do
122         begin
123           go[p,c]:=nq;
124           p:=f[p];
125         end;
126       end;
127     end;
128   end;
129
130 procedure make(l,r,m:longint);
131   var i,j:longint;
132   begin
133     for j:=1 to m do
134     begin
135       p[j]:=0;
136       for i:=l to r do
137         p[j]:=p[j]+g[i,j]*(1 shl (i-l));
138     end;
139   end;
140
141 begin
142   readln(n,m,a,b);
143   if a>4 then
144   begin
145     for i:=1 to n do
146       readln(c[i]);
147     readln(te);
148     while te>0 do
149     begin
150       dec(te);
151       j:=0;
152       trie[0,‘1‘]:=0;
153       trie[0,‘0‘]:=0;
154       t:=0;
155       len:=0;
156       for i:=1 to a do
157       begin
158         readln(s);
159         j:=0;
160         for k:=1 to b do
161         begin
162           if trie[j,s[k]]=0 then
163           begin
164             inc(t); p[t]:=0; w[t]:=k;
165             trie[t,‘0‘]:=0; trie[t,‘1‘]:=0;
166             trie[j,s[k]]:=t;
167           end;
168           j:=trie[j,s[k]];
169         end;
170         add(j,i);
171       end;
172       ac;
173       for i:=0 to t do
174         for ch:=‘0‘ to ‘1‘ do
175         begin
176           j:=i;
177           while (j>0) and (trie[j,ch]=0) do j:=f[j];
178           j:=trie[j,ch];
179           po[i,ch]:=j;
180         end;
181
182       ans:=0;
183       for i:=1 to n do
184       begin
185         j:=0;
186         for k:=1 to m do
187         begin
188           if not((c[i][k]>=‘0‘) and (c[i][k]<=‘1‘)) then break;
189           if b-w[j]>m-k+1 then break;
190           j:=po[j,c[i][k]];
191           if p[j]<>0 then
192           begin
193             fill(i,k,j);
194             if ans=a then break;
195           end;
196         end;
197         if (ans=a) or (n-i+ans<a) then break;
198       end;
199       writeln(ans div a);
200     end;
201   end
202   else begin
203     t:=1;
204     for i:=1 to n do
205     begin
206       for j:=1 to m do
207       begin
208         read(ch);
209         g[i,j]:=ord(ch)-48;
210       end;
211       readln;
212     end;
213     for i:=1 to n-a+1 do
214     begin
215       make(i,i+a-1,m);
216       last:=1;
217       for j:=1 to m do
218         if go[last,p[j]]<>0 then change(p[j])
219         else ins(p[j]);
220     end;
221     readln(te);
222     while te>0 do
223     begin
224       dec(te);
225       for i:=1 to a do
226       begin
227         for j:=1 to b do
228         begin
229           read(ch);
230           g[i,j]:=ord(ch)-48;
231         end;
232         readln;
233       end;
234       make(1,a,b);
235       j:=1;
236       ans:=1;
237       for i:=1 to b do
238         if go[j,p[i]]=0 then
239         begin
240           ans:=0;
241           break;
242         end
243         else j:=go[j,p[i]];
244       writeln(ans);
245     end;
246   end;
247 end.

时间: 2024-08-24 16:38:45

bzoj2351 2462的相关文章

BZOJ 2462: [BeiJing2011]矩阵模板

2462: [BeiJing2011]矩阵模板 Time Limit: 2 Sec  Memory Limit: 128 MBSubmit: 915  Solved: 432[Submit][Status][Discuss] Description 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过.    所谓01矩阵,就是矩阵中所有元素不是0就是1. Input 输入文件的第一行为M.N.A.B,参见题目描述. 接下来M行,每行N个字符,非0即1

暖心!阿里安全白帽挖漏洞做公益 连收2462封山区小孩感谢信

"你们给我捐的书包里面有我们非常需要的文具--,原来没有这些东西的时候,我们到处去借,--我知道你们是自己辛苦赚来的钱,买这些东西来支助我们,谢谢对我们学习的关心."近日,阿里巴巴安全部安全专家木雁收到一份特别的礼物--整整2462封感谢信.木雁从未想过,自己挖漏洞获奖将24.62万元奖金捐给山区孩子买文具的举动,会得到这么多回信. 他回忆称,两年前自己参加了由阿里联合厂商举办的先知公益众测项目,给第三方厂商提交13个漏洞而获得这笔奖金,并最终通过中国扶贫基金会,给重庆偏远山区孩子购买

HDU 2462 The Luckiest number

传送门 题目大意: 定义只含有数字8的数为幸运数 给定正整数L,求L的所有倍数中最小的幸运数 算法思路: 设最终答案为x个8,则x满足(10x-1)*8/9≡0 (mod L) 化简:10x≡1 (mod n),其中n=9L/gcd(9L,8) 这是一个离散对数的问题,求解方法如下: 若gcd(10,n)>1,无解 若gcd(10,n)=1,由欧拉定理:10?(n)≡1 (mod n).可以证明,x为?(n)的约数,从小到大枚举约数即可 10l≡1 (mod n) 则成立的最小 l 是? (n)

The Luckiest number(hdu 2462)

给定一个数,判断是否存在一个全由8组成的数为这个数的倍数 若存在则输出这个数的长度,否则输出0 /* 个人感觉很神的一道题目. 如果有解的话,会有一个p满足:(10^x-1)/9*8=L*p => 10^x-1=9*L*p/8 设m=9*L/gcd(L,8) 则存在p1使得 10^x-1=m*p1 => 10^x=1(mod m) 根据欧拉定理 10^φ(m)=1(mod m) 所以x一定是φ(m)的因数(这好像是某个定理). */ #include<iostream> #incl

【字符矩阵哈希】bzoj2351 [BeiJing2011]Matrix

引用题解:http://blog.csdn.net/popoqqq/article/details/41084047 #include<cstdio> #include<cstring> using namespace std; typedef unsigned long long ull; int n,m,a,b,q; const ull seed1=17,seed2=19; #define MOD 1000001 ull v[MOD],sum[1001][1001],ord[2

BZOJ2351: [BeiJing2011]Matrix

2351: [BeiJing2011]Matrix Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 589  Solved: 171[Submit][Status] Description 给定一个M行N列的01矩阵,以及Q个A行B列的01矩阵,你需要求出这Q个矩阵哪些在原矩阵中出现过. 所谓01矩阵,就是矩阵中所有元素不是0就是1. Input 输入文件的第一行为M.N.A.B,参见题目描述. 接下来M行,每行N个字符,非0即1,描述原矩阵. 接

BZOJ 2462 BeiJing 2011 矩阵模板 二维hash

题目大意:给出一个m*n的由01组成的矩阵,下面有q个询问,查询矩阵中存不存在大小为k*l的子矩阵. 思路:二维hash.我们先把大矩阵hash,然后把所有可能的k*l的子矩阵都插到哈希表里,然后只要对于每个询问hash一下看哈希表中是否存在. 值得一提的是,这个题只需要输出10个1就可以AC了.. CODE: #include <cstdio> #include <bitset> #include <cstring> #include <iostream>

【转】Windows Error Code(windows错误代码详解)

本文来自: http://blog.sina.com.cn/s/blog_5e45d1be0100i0dr.html http://blog.sina.com.cn/s/blog_5e45d1be0100i0dt.html http://blog.sina.com.cn/s/blog_5e45d1be0100i0dv.html 这三篇,因为格式实在太乱,因此拿来整理了一下.找这个的原因是今天在改程序的时候蹦出来个WindowsError: [Error 2],也没有说这个东西是什么错误.于是百度

【2016-11-2】【坚持学习】【Day17】【微软 推出的SQLHelper】

从网络上找到 微软原版本的SQLHelper,很多行代码.认真看了,学习了. 代码: 1 using System; 2 using System.Data; 3 using System.Xml; 4 using System.Data.SqlClient; 5 using System.Collections; 6 7 namespace Helper 8 { 9 /// <summary> 10 /// The SqlHelper class is intended to encapsu