UVA 11019 Matrix Matcher

题意:找在矩阵T中P矩阵出现的次数

做法:AC 自动机,或者hash。

hash版:http://blog.csdn.net/moon_no2015/article/details/47760299

AC自动机版:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define ll long long
#define nn 100100

int sz;
int ch[nn][26];//建树
int val[nn];//记录尾节点
int f[nn];//表示i失配时转移到的新状态
int last[nn];//表示节点i沿着失配指针往回走时,遇到的下个单词节点编号
int pos[160];//与第i行相同的行
int cnt[1010][1010];

struct Aho
{
	void init()
	{
		sz = 1;
		memset(ch[0], 0, sizeof(ch[0]));
		memset(cnt, 0, sizeof(cnt));
		memset(pos, 0, sizeof(pos));
		val[0] = 0;
	}
	int idx(char c)
	{
		/*
		if (c >= '0' && c <= '9') return c - '0';
		else if (c >= 'a'&& c <= 'z') return c - 'a' + 10;
		else return c - 'A' + 36;*/
		return c - 'a';
	}

	void insert(char* s,int v)
	{
		int u = 0, len = strlen(s);
		for (int i = 0; i < len; i++)
		{
			int c = idx(s[i]);
			if (!ch[u][c])
			{
				memset(ch[sz], 0, sizeof(ch[sz]));
				val[sz] = 0;
				ch[u][c] = sz++;
			}
			u = ch[u][c];
		}
		if (val[u]) pos[v] = val[u];
		val[u] = v;
	}

	void get_f()
	{
		queue<int>q;
		f[0] = 0;
		for (int c = 0; c < 26; c++)
		{
			int u = ch[0][c];
			if (u)
			{
				f[u] = 0;
				q.push(u);
				last[u] = 0;
			}
		}
		while (!q.empty())
		{
			int r = q.front();
			q.pop();
			for (int c = 0; c < 26; c++)
			{
				int u = ch[r][c];
				if (!u)
				{
					ch[r][c] = ch[f[r]][c];
					continue;
				}
				q.push(u);
				int v = f[r];
				while (v && !ch[v][c]) v = f[v];
				f[u] = ch[v][c];
				last[u] = val[f[u]] ? f[u] : last[f[u]];
			}
		}
	}
	void print(int i, int j, int x)
	{
		if (j)
		{
			if (x - val[j] + 1 > 0)
				cnt[x - val[j] + 1][i] ++;
			int t = val[j];
			while (pos[t])
			{
				t=pos[t];
				if (x - t + 1 > 0) cnt[x - t + 1][i]++;
			}
			print(i, last[j], x);
		}
	}
	void find(char* s,int x)
	{
		int n = strlen(s);
		int j = 0;
		for (int i = 0; i < n; i++)
		{
			int c = idx(s[i]);
			j = ch[j][c];
			if (val[j]) print(i, j, x);
			else if (last[j]) print(i, last[j], x);
		}
	}
};
char t[1010][1010],p[110][110];
int main()
{
	Aho ac;
	int tt, n, m, x, y;
	scanf("%d", &tt);
	while (tt--)
	{
		ac.init();
		scanf("%d%d", &n,&m);
		for (int i = 1; i <= n; i++)
			scanf("%s", t[i]);
		scanf("%d%d", &x,&y);
		for (int i = 1; i <= x; i++)
		{
			scanf("%s", p[i]);
			ac.insert(p[i], i);
		}
		ac.get_f();
		for (int i = 1; i <= n; i++)
			ac.find(t[i],i);
		int ans = 0;
		for (int i = 1; i <= n; i++)
			for (int j = 0; j < m; j++)
				if (cnt[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、付费专栏及课程。

余额充值