Matrix Matcher UVA11019

83 篇文章 0 订阅
55 篇文章 0 订阅



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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java Matcher是Java中的一个类,用于对字符串进行匹配操作。它是通过正则表达式来实现字符串匹配的功能。Matcher类提供了一系列方法,可以用于查找、匹配和替换字符串。 Matcher类的常用方法包括: 1. `matches()`:尝试将整个输入序列与模式进行匹配。 2. `find()`:在输入序列中查找下一个匹配项。 3. `group()`:返回与上一次匹配操作相匹配的输入子序列。 4. `start()`:返回上一次匹配操作的起始索引。 5. `end()`:返回上一次匹配操作的结束索引。 使用Matcher类需要先创建一个Pattern对象,通过Pattern对象的`matcher()`方法来创建一个Matcher对象。然后可以使用Matcher对象的方法进行字符串匹配操作。 下面是一个示例代码,演示了如何使用Matcher类进行字符串匹配: ```java import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherExample { public static void main(String[] args) { String input = "Hello, World! This is a test string."; String pattern = "\\b\\w+\\b"; // 匹配单词 Pattern p = Pattern.compile(pattern); Matcher m = p.matcher(input); while (m.find()) { System.out.println("Match: " + m.group()); System.out.println("Start index: " + m.start()); System.out.println("End index: " + m.end()); } } } ``` 输出结果: ``` Match: Hello Start index: 0 End index: 5 Match: World Start index: 7 End index: 12 Match: This Start index: 15 End index: 19 Match: is Start index: 20 End index: 22 Match: a Start index: 23 End index: 24 Match: test Start index: 25 End index: 29 Match: string Start index: 30 End index: 36 ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值