Matrix Matcher UVA11019



#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>   
#include <map>
#include <string>  
#include <climits> 
#include <set>
#include <string>    
#include <sstream>
#include <utility>   
#include <ctime>

using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::istringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PAIR;

const int MAXN(10010);
const int SIGMA_SIZE(130);
const int MAXM(110);
const int MAXE(200010);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 2);
const int BASE(131);
const ULL LIM(1000000000000000);

int table[1010][1010];

struct AC
{
	int ch[MAXN][SIGMA_SIZE];
	vector<int> val[MAXN];
	int f[MAXN];
	int last[MAXN];
	int size;
	void init()
	{
		memset(ch[0], 0, sizeof(ch[0]));
		f[0] = last[0] = 0;
		val[0].clear();
		size = 1;
	}

	inline int idx(char temp)
	{
		return temp;
	}

	void insert(char *S, int tv)
	{
		int u = 0, id;
		for(; *S; ++S)
		{
			id = idx(*S);
			if(!ch[u][id])
			{
				memset(ch[size], 0, sizeof(ch[size]));
				val[size].clear();
				ch[u][id] = size++;
			}
			u = ch[u][id];
		}
		val[u].push_back(tv);
	}
	
	int que[MAXN];
	int front, back;
	void construct()
	{
		front = back = 0;
		int cur, u;
		for(int i = 0; i < SIGMA_SIZE; ++i)
		{
			u = ch[0][i];
			if(u)
			{
				que[back++] = u;
				f[u] = last[u] = 0;
			}
		}
		
		while(front < back)
		{
			cur = que[front++];
			for(int i = 0; i < SIGMA_SIZE; ++i)
			{
				u = ch[cur][i];
				if(u)
				{
					que[back++] = u;
					f[u] = ch[f[cur]][i];
					last[u] = val[f[u]].empty()? last[f[u]]: f[u];
				}
				else
					ch[cur][i] = ch[f[cur]][i];
			}
		}
	}
	
	void find(char *T, int row)
	{
		int pi = 0, id, ind = 0;
		for(; *T; ++T, ++ind)
		{
			id = idx(*T);
			pi = ch[pi][id];
			for(int i = pi; i; i = last[i])
			{
				int ts = val[i].size();
				for(int j = 0; j < ts; ++j)
				{
					int temp = row-val[i][j];
					if(temp > 0)
						++table[temp][ind];
				}
			}
		}
	}
};

AC ac;

char str[1010][1010];

int main()
{
	int TC;
	scanf("%d", &TC);
	while(TC--)
	{
		int n, m, x, y;
		scanf("%d%d", &n, &m);
		for(int i = 1; i <= n; ++i)
			scanf("%s", str[i]);
		scanf("%d%d", &x, &y);
		ac.init();
		for(int i = 0; i < x; ++i)
		{
			scanf("%s", str[0]);
			ac.insert(str[0], i);
		}
		ac.construct();
		memset(table, 0, sizeof(table[0])*(n+1));
		for(int i = 1; i <= n; ++i)
			ac.find(str[i], i);
		int ans = 0;
		for(int i = 1; i <= n; ++i)
			for(int j = 1; j <= m; ++j)
				if(table[i][j] == x)
					++ans;
		printf("%d\n", ans);
	}
	return 0;
}


你可能感兴趣的:(Matrix Matcher UVA11019)