题意:给出一个n*m的字符矩阵T,你的任务是找出给定的x*y的字符矩阵P在T中出现了多少次.
分析:白书原题,先把P构造成一个AC自动机,然后在T中逐行匹配。用一个数组记录以一个点为左上角能匹配到P的多少行。
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#define MAXNODE 10005
using namespace std;
int T,n,m,x,y;
char s[105][105],t[1005][1005];
struct Acautomata
{
int ch[MAXNODE][26];
int f[MAXNODE];
vector<int> val[MAXNODE];
int got[1005][1005];
int tot;
void init()
{
tot = 1;
memset(ch[0],0,sizeof(ch[0]));
memset(got,0,sizeof(got));
}
int idx(char c)
{
return c - 'a';
}
void insert(char *s,int v)
{
int u = 0,n = strlen(s);
for(int i = 0;i < n;i++)
{
int c = idx(s[i]);
if(!ch[u][c])
{
memset(ch[tot],0,sizeof(ch[tot]));
val[tot].clear();
ch[u][c] = tot++;
}
u = ch[u][c];
}
val[u].push_back(