LINK1

LINK2


题目大意

让你在一个大小为\(n*m\)的矩阵中找大小是\(x*y\)的矩阵的出现次数

思路1:Hash

hash思路及其傻逼

你把一维情况扩展一下

一维是一个bas,那你二维就用两个bas好了

对一个在\((i,j)\)的字符,令他的hash值是\(c_{i,j}*bas1^i*bas2^j\)

然后算出矩阵hash值乘上差量判断就做完了

70ms


//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
typedef pair<int, int> pi;
typedef long long ll;
typedef double db;
#define fi first
#define se second
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e3 + 10;
const int Mod = 998244353;
const int bas1 = 233333;
const int bas2 = 19260817; int pow1[N], pow2[N];
int n, m, x, y;
char s[N][N], c[N][N];
int sum[N][N], val; int add(int a, int b) {
return (a += b) >= Mod ? a - Mod : a;
} int mul(int a, int b) {
return 1ll * a * b % Mod;
} int sub(int a, int b) {
return (a -= b) < 0 ? a + Mod : a;
} int fast_pow(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = mul(res, a);
b >>= 1;
a = mul(a, a);
}
return res;
} void init() {
pow1[0] = pow2[0] = 1;
fu(i, 1, N - 1) pow1[i] = mul(bas1, pow1[i - 1]);
fu(i, 1, N - 1) pow2[i] = mul(bas2, pow2[i - 1]);
} void getsum() {
fu(i, 1, n)
fu(j, 1, m)
sum[i][j] = add(sub(add(sum[i][j - 1], sum[i - 1][j]), sum[i - 1][j - 1]), mul(s[i][j], mul(pow1[i], pow2[j])));
val = 0;
fu(i, 1, x)
fu(j, 1, y) val = add(val, mul(c[i][j], mul(pow1[i], pow2[j])));
} void solve() {
Read(n), Read(m);
fu(i, 1, n) scanf("%s", s[i] + 1);
Read(x), Read(y);
fu(i, 1, x) scanf("%s", c[i] + 1);
getsum();
int ans = 0;
fu(i, x, n)
fu(j, y, m)
if (sub(add(sum[i][j], sum[i - x][j - y]), add(sum[i][j - y], sum[i - x][j])) == mul(val, mul(pow1[i - x], pow2[j - y])))
++ans;
Write(ans), putchar('\n');
} int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
int T; Read(T);
init();
while (T--) solve();
return 0;
}

思路2:AC自动机

用AC自动机来考虑的话这题挺好的

虽然跑600ms

考虑一下把模式串分解变成x个长度是y的串

然后全部塞进AC自动机

然后考虑算出在\(n*m\)的矩阵中有哪些串在哪些位置出现过

这个东西跑一边就可以处理出来

如果有不好处理的细节你就想怎么暴力怎么来

然后我们考虑假如在\((i,j)\)这个位置匹配到了第k行

那么对于左上角在\((i-k,j)\)的矩阵显然是可以匹配第k行的

那么我们就记录一下每个节点是左上角的矩阵最多能匹配多少行就可以了


//Author: dream_maker
#include<bits/stdc++.h>
using namespace std;
//----------------------------------------------
typedef pair<int, int> pi;
typedef long long ll;
typedef double db;
#define fi first
#define se second
#define fu(a, b, c) for (int a = b; a <= c; ++a)
#define fd(a, b, c) for (int a = b; a >= c; --a)
#define fv(a, b) for (int a = 0; a < (signed)b.size(); ++a)
const int INF_of_int = 1e9;
const ll INF_of_ll = 1e18;
template <typename T>
void Read(T &x) {
bool w = 1;x = 0;
char c = getchar();
while (!isdigit(c) && c != '-') c = getchar();
if (c == '-') w = 0, c = getchar();
while (isdigit(c)) {
x = (x<<1) + (x<<3) + c -'0';
c = getchar();
}
if (!w) x = -x;
}
template <typename T>
void Write(T x) {
if (x < 0) {
putchar('-');
x = -x;
}
if (x > 9) Write(x / 10);
putchar(x % 10 + '0');
}
//----------------------------------------------
const int N = 1e3 + 10;
const int CHARSET_SIZE = 26;
struct Node {
int ch[CHARSET_SIZE], fail;
int id[110];
Node() {}
void clean() {
fu(i, 0, CHARSET_SIZE - 1) ch[i] = 0;
id[0] = fail = 0;
}
} p[N * N];
int n, m, x, y, cnt;
int res[N][N];
char s[N][N], c[N][N]; void init() {
cnt = 1;
p[0].clean(); p[1].clean();
fu(i, 0, CHARSET_SIZE - 1) p[0].ch[i] = 1;
} void insert(char *c, int id) {
int u = 1;
fu(i, 1, y) {
int tmp = c[i] - 'a';
if (!p[u].ch[tmp])
p[p[u].ch[tmp] = ++cnt].clean();
u = p[u].ch[tmp];
}
p[u].id[++p[u].id[0]] = id;
} void build_fail() {
static queue<int> q;
q.push(1);
while (q.size()) {
int u = q.front(); q.pop();
fu(i, 0, CHARSET_SIZE - 1) {
int w = p[u].ch[i], v = p[u].fail;
while (!p[v].ch[i]) v = p[v].fail;
v = p[v].ch[i];
if (w) {
p[w].fail = v;
q.push(w);
} else p[u].ch[i] = v;
}
}
} void trans(char *c, int id) {
int u = 1;
fu(i, 1, m) {
u = p[u].ch[c[i] - 'a'];
fu(j, 1, p[u].id[0]) {
if (id >= p[u].id[j])
res[id - p[u].id[j] + 1][i]++;
}
}
} void solve() {
init();
Read(n), Read(m);
fu(i, 1, n) scanf("%s", s[i] + 1);
Read(x), Read(y);
fu(i, 1, x) {
scanf("%s", c[i] + 1);
insert(c[i], i);
}
build_fail();
fu(i, 1, n) fu(j, 1, m) res[i][j] = 0;
fu(i, 1, n) trans(s[i], i);
int ans = 0;
fu(i, 1, n)
fu(j, 1, m) if (res[i][j] == x) ++ans;
Write(ans), putchar('\n');
} int main() {
#ifdef dream_maker
freopen("input.txt", "r", stdin);
#endif
int T; Read(T);
while (T--) solve();
return 0;
}

UVA11019 Matrix Matcher【hash傻逼题】【AC自动机好题】的更多相关文章

  1. HDU 3065 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3065 题目大意:多个模式串,范围是大写字母.匹配串的字符范围是(0~127).问匹配串中含有哪几种模 ...

  2. 经典算法题每日演练——第八题 AC自动机

    原文:经典算法题每日演练--第八题 AC自动机 上一篇我们说了单模式匹配算法KMP,现在我们有需求了,我要检查一篇文章中是否有某些敏感词,这其实就是多模式匹配的问题. 当然你也可以用KMP算法求出,那 ...

  3. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  4. HDU 2896 (AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2896 题目大意:多个模式串.多个匹配串.其中串的字符范围是(0~127).问匹配串中含有哪几个模式串 ...

  5. HDU 2222(AC自动机模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...

  6. HDU3695(AC自动机模板题)

    题意:给你n个字符串,再给你一个大的字符串A,问你着n个字符串在正的A和反的A里出现多少个? 其实就是AC自动机模板题啊( ╯□╰ ) 正着query一次再反着query一次就好了 /* gyt Li ...

  7. hdu2222 KeyWords Search AC自动机入门题

    /** 链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:题意:给定N(N <= 10000)个长度不大于50的模式串,再给定一个长度为L ...

  8. HDu-2896 病毒侵袭,AC自动机模板题!

    病毒侵袭 模板题,不多说了.. 题意:n个不同的字符串分别代表病毒特征,给出m次查询,每次一个字符串(网址),求这个字符串中有几个病毒特征,分别从大到小输出编号,最后输出所有的带病毒网址个数.格式请看 ...

  9. [Bzoj3940] [AC自动机,USACO 2015 February Gold] Censor [AC自动机模板题]

    AC自动机模板题(膜jcvb代码) #include <iostream> #include <algorithm> #include <cstdio> #incl ...

  10. hdu 2222(AC自动机模版题)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. hdu5072 容斥+枚举

    这题说的是给了 n 个数字 每个数值大于1 小于100000,n小于100000 ,找出满足下面要求的三人组有多少种 比如abc ( (ab)==(bc)==(ac) ==1 )||( (ab)!=1 ...

  2. qml学习笔记(二):可视化元素基类Item详解(上半场anchors等等)

    原博主博客地址:http://blog.csdn.net/qq21497936本文章博客地址:http://blog.csdn.net/qq21497936/article/details/78516 ...

  3. LA 7277 Landscaping(最小割)

    https://vjudge.net/problem/UVALive-7277 题意: 给出一个n*m的地图,.代表低坡,#代表高坡. 现在有n+m辆车分别从上端和左端出发,如果在行驶的过程中需要转换 ...

  4. UVa 11021 麻球繁衍

    https://vjudge.net/problem/UVA-11021 题意:有k只麻球,每只活一天就会死亡,临死之前可能会生出一些新的麻球.具体来说,生i个麻球的概率为Pi.给定m,求m天后所有麻 ...

  5. Solidity 官方文档中文版 4_Solidity 编程实例

    Voting 投票 接下来的合约非常复杂,但展示了很多Solidity的特性.它实现了一个投票合约.当然,电子选举的主要问题是如何赋予投票权给准确的人,并防止操纵.我们不能解决所有的问题,但至少我们会 ...

  6. python 简单的猜数字游戏

    !/usr/bin/env python --encoding:utf-8-- import random think=random.randint(1,10) print ("...... ...

  7. python 列表元素替换以及删除

    >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] >>> letters ['a', 'b', 'c', 'd', ...

  8. mybatis 环境搭建和基本说明

    mybatis介绍就不多提了,直接步入正题. 先准备好eclipse和MySQL,然后先看一下目录结构 文件和类很少,所以mybatis的搭建是非常简单的,如搭建中遇到问题可以先参考文档最后一部分的综 ...

  9. Android之修改用户头像并上传服务器(实现手机拍照和SD卡选择上传)

    写了这么多个的APP,最近才把他这个功能写上来,就抽取其中的用户修改头像的相关操作这个功能写了这篇博客,来与大家分享,希望对你有所帮助. 案例包含了: Xutil图片上传 拍照和SD卡选择图片 图片缓 ...

  10. Linux Mint 18.1安装nvidia驱动

    硬件环境:Dell Inspiron 7557笔记本(i7,8G,GTX960M) 软件环境:Linux Mint 18.1(基于Ubuntu 16.04) 问题描述: Linux Mint 18.1 ...