题目链接

序列上的Hash和前缀和差不多,二维Hash也和二维前缀和差不多了。

预处理大矩阵所有r*c的小矩阵hash值,再对询问的矩阵Hash。

类比于序列上\(s[r]-s[l-1]*pow[r-l+1]\),比如\(s[i-r][j-c]\)多算了\(r*c\)次,乘个\(pow[r]*pow[c]\)就行。

用指针替掉数组的一维竟然慢点。。好吧毕竟也就一维,数据也不大。

//106864kb	1520ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 500000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define P 100000000
#define base1 12289
#define base2 786433
typedef unsigned int uint;//在这用unsigned long long好像没太大用吧...果然,光荣地T了
const int N=1005; uint A[N][N],sum[N][N],pw1[N],pw2[N];
bool Hash[P];
char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int read01()
{
register char c=gc();
for(;!isdigit(c);c=gc());
return c-'0';
} int main()
{
int n=read(),m=read(),r=read(),c=read();
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j) sum[i][j]=read01();
pw1[0]=pw2[0]=1;
for(int i=1; i<=n; ++i) pw1[i]=pw1[i-1]*base1;
for(int i=1; i<=m; ++i) pw2[i]=pw2[i-1]*base2;
for(int i=2; i<=n; ++i)//Row
for(int j=1; j<=m; ++j) sum[i][j]+=sum[i-1][j]*base1;
for(int i=1; i<=n; ++i)//Column
for(int j=2; j<=m; ++j) sum[i][j]+=sum[i][j-1]*base2;
for(int i=r; i<=n; ++i)
for(int j=c; j<=m; ++j)
{
uint hash=sum[i][j]-sum[i-r][j]*pw1[r]-sum[i][j-c]*pw2[c]+sum[i-r][j-c]*pw1[r] *pw2[c];
Hash[hash%P]=1;
}
for(int Q=read(); Q--; )
{
for(int i=1; i<=r; ++i)
for(int j=1; j<=c; ++j) A[i][j]=read01();
for(int i=2; i<=r; ++i)
for(int j=1; j<=c; ++j) A[i][j]+=A[i-1][j]*base1;
for(int i=1; i<=r; ++i)
for(int j=2; j<=c; ++j) A[i][j]+=A[i][j-1]*base2;
puts(Hash[A[r][c]%P]?"1":"0");
} return 0;
}
//106864kb	1544ms
#include <cstdio>
#include <cctype>
#include <algorithm>
//#define gc() getchar()
#define MAXIN 500000
#define gc() (SS==TT&&(TT=(SS=IN)+fread(IN,1,MAXIN,stdin),SS==TT)?EOF:*SS++)
#define P 100000000
#define base1 12289
#define base2 786433
typedef unsigned int uint;//在这用unsigned long long好像没太大用吧... 果然,光荣地T了
const int N=1005; uint A[N][N],sum[N][N],pw1[N],pw2[N];
bool Hash[P];
char IN[MAXIN],*SS=IN,*TT=IN; inline int read()
{
int now=0;register char c=gc();
for(;!isdigit(c);c=gc());
for(;isdigit(c);now=now*10+c-'0',c=gc());
return now;
}
inline int read01()
{
register char c=gc();
for(;!isdigit(c);c=gc());
return c-'0';
} int main()
{
int n=read(),m=read(),r=read(),c=read();
for(int i=1; i<=n; ++i)
for(int j=1; j<=m; ++j) sum[i][j]=read01();
pw1[0]=pw2[0]=1;
for(int i=1; i<=n; ++i) pw1[i]=pw1[i-1]*base1;
for(int i=1; i<=m; ++i) pw2[i]=pw2[i-1]*base2;
for(int i=2; i<=n; ++i)//Row
{
uint *s=sum[i], *las=sum[i-1];
for(int j=1; j<=m; ++j) s[j]+=las[j]*base1;
}
for(int i=1; i<=n; ++i)//Column
{
uint *s=sum[i];
for(int j=2; j<=m; ++j) s[j]+=s[j-1]*base2;
}
for(int i=r; i<=n; ++i)
for(int j=c; j<=m; ++j)
{
uint hash=sum[i][j]-sum[i-r][j]*pw1[r]-sum[i][j-c]*pw2[c]+sum[i-r][j-c]*pw1[r]*pw2[c];
Hash[hash%P]=1;
}
for(int Q=read(); Q--; )
{
for(int i=1; i<=r; ++i)
for(int j=1; j<=c; ++j) A[i][j]=read01();
for(int i=2; i<=r; ++i)
{
uint *s=A[i], *las=A[i-1];
for(int j=1; j<=c; ++j) s[j]+=las[j]*base1;
}
for(int i=1; i<=r; ++i)
{
uint *s=A[i];
for(int j=2; j<=c; ++j) s[j]+=s[j-1]*base2;
}
puts(Hash[A[r][c]%P]?"1":"0");
} return 0;
}

最新文章

  1. 教你快速高效接入SDK——服务器端支付回调的处理方式
  2. ios8中,相册创建后手动删除,不能再进行创建显示
  3. Uploadify 3.2 上传图片
  4. The Glorious Karlutka River =)
  5. Ubuntu设置环境变量的几种方法
  6. CSS知识点摘记
  7. 体验VS2017的Live Unit Testing
  8. JDBC(四)
  9. supervisor安装使用和我踩过的坑
  10. linux shell set命令
  11. 爬虫系列3:Requests+Xpath 爬取租房网站信息并保存本地
  12. Android 真机调试
  13. tp框架中 关于数据库mysql 的一些疑点知识
  14. module模块和包
  15. XSS事件(一)
  16. (NOI2014)(bzoj3669)魔法森林
  17. Java HashMap 遍历方式探讨
  18. &lt;深入理解JavaScript&gt;学习笔记(1)_编写高质量JavaScript代码的基本要点
  19. android基础----&gt;AccessibilityService的简单使用(一)
  20. Step by Step 使用HTML5开发一个星际大战游戏(1)

热门文章

  1. 带你正确的使用List的retainAll方法求交集
  2. SQL语句(十四)子查询
  3. [转载]Understanding the Bootstrap 3 Grid System
  4. asp.net C#母版页和内容页事件排版加载顺序生命周期
  5. UVALive 7456 Least Crucial Node
  6. 因子分析(Factor analysis)
  7. python实现两个经纬度点之间的距离和方位角
  8. 【Python】Flask系列-模板笔记
  9. arm GIC介绍之一【转】
  10. ioctl函数详细说明(网络)