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;
}

最新文章

  1. EXCEL导入(反射)
  2. SVN服务器搭建和使用(三)(转载)
  3. QT设置窗口屏幕居中
  4. Codeforces Round #200 (Div. 1)D. Water Tree dfs序
  5. adb logcat 查看日志
  6. PCB模擬設計接地的指導原則
  7. 腾讯AlloyTeam正式发布omi-cli脚手架 v1.0 - 创建网站无需任何配置
  8. jenkins+ant+jmeter接口自动化测试(持续构建)
  9. windows转mac-开发环境搭建(一):需要搭建的环境及安装的工具
  10. 4999: This Problem Is Too Simple!
  11. Visual Studio2012调试时无法命中断点
  12. Flv视频格式如何转换成MP4格式
  13. Ajax中返回数据的格式
  14. 编译Caffe出错,解决方案记录
  15. 5 Protocols For Event-Driven API Architectures
  16. linux shell脚本检测硬盘磁盘空间 邮件报警
  17. BZOJ.1926.[SDOI2010]粟粟的书架(前缀和 主席树 二分)
  18. Libssh认证绕过CVE-2018-10933漏洞复现
  19. 洛谷P3760异或和
  20. spring注解@Value取不到值【转】

热门文章

  1. 线程,协程,IO模型
  2. 3.12 Templates -- Wrting Helpers(编写辅助器)
  3. Winform开发之ComboBox和ComboBoxEdit控件绑定key/value数据
  4. hdu5106 数位dp
  5. flex与j2ee的结合(flex+Spring)
  6. 使用Linux重定向解决nohup.out无写权限问题
  7. .net core 2.2 &amp; Mongodb
  8. SQL学习笔记四(补充-1)之MySQL单表查询
  9. PHP设计模式_注册树模式
  10. pybedtools --bedtools的python包